how copy the file which in my project directory to the .exe file directory in windows



  • Hi:
    I develop on windows using qt,I just want to copy a file which named '1.txt' to the directory which the exe file in,I had read the 'installing files' document in here : http://doc.qt.io/qt-5/qmake-advanced-usage.html, it seem work on the Unix, and test it using my project in windows,the INSTALLS not work which just as I predict:

    here is my .pro file:

    ROOT_DIRECTORY = $$PWD
    LIB_DIRECTORY = $${ROOT_DIRECTORY}/lib
    BUILD_DIRECTORY = $${ROOT_DIRECTORY}/build
    TESTS_DIRECTORY = $${BUILD_DIRECTORY}/tests
    EXAMPLES_DIRECTORY = $${BUILD_DIRECTORY}/examples
    EXEC_DIRECTORY = $${BUILD_DIRECTORY}

    #$${LIB_DIRECTORY}
    DESTDIR = $${EXEC_DIRECTORY}
    OBJECTS_DIR = $${BUILD_DIRECTORY}
    MOC_DIR = $${BUILD_DIRECTORY}
    RCC_DIR = $${BUILD_DIRECTORY}

    install_it.path = $$DESTDIR/
    install_it.files += $$PWD/Files/1.txt

    INSTALLS += install_it

    what should I do if I want to copy the file which in my project directory to the .exe file directory in windows,thanks a lot!


  • Qt Champions 2018

    @Princein You can simply create a resource file and put your txt file there. See http://doc.qt.io/qt-5/resources.html



  • @Princein

    Is this what you are looking for https://stackoverflow.com/a/36438513?
    Another method here .
    Alternatively you can use copy http://doc.qt.io/qt-5/qfile.html#copy-1 if file does not exists else you need to check if exists and remove them and then copy.


  • Moderators

    The above links are usefull and correct, I'll add a small function, inside the *.pro file, I'll use to copy files

    DESTDIR     = deployment
    
    ...
    
    # copies the given files to the destination directory
    defineTest(copyToDestDir) {
        files = $$1
        dir = $$2
        # replace slashes in destination path for Windows
        win32:dir ~= s,/,\\,g
    
        for(file, files) {
            # replace slashes in source path for Windows
            win32:file ~= s,/,\\,g
    
            QMAKE_POST_LINK += $$QMAKE_COPY_DIR $$shell_quote($$file) $$shell_quote($$dir) $$escape_expand(\\n\\t)
        }
    
        export(QMAKE_POST_LINK)
    }
    
    #Use
    copyToDestDir($$PWD/translations, deployment/translations)
    


  • @J.Hilk

    I had try it in my windows qt project,but it not work for me and I don't no why,in my project there is a subDirectory which named 'Files' and a 1.txt file in it, here is the .pro file :

    ROOT_DIRECTORY = $$PWD
    LIB_DIRECTORY = $${ROOT_DIRECTORY}/lib
    BUILD_DIRECTORY = $${ROOT_DIRECTORY}/build
    TESTS_DIRECTORY = $${BUILD_DIRECTORY}/tests
    EXAMPLES_DIRECTORY = $${BUILD_DIRECTORY}/examples
    EXEC_DIRECTORY = $${BUILD_DIRECTORY}

    #$${LIB_DIRECTORY}
    DESTDIR = $${EXEC_DIRECTORY}
    OBJECTS_DIR = $${BUILD_DIRECTORY}
    MOC_DIR = $${BUILD_DIRECTORY}
    RCC_DIR = $${BUILD_DIRECTORY}

    copies the given files to the destination directory

    defineTest(copyToDestDir) {
    files = $$1
    dir = $$2
    # replace slashes in destination path for Windows
    win32:dir ~= s,/,\,g

    for(file, files) {
        # replace slashes in source path for Windows
        win32:file ~= s,/,\\,g
    
        QMAKE_POST_LINK += $$QMAKE_COPY_DIR $$shell_quote($$file) $$shell_quote($$dir) $$escape_expand(\\n\\t)
    }
    
    export(QMAKE_POST_LINK)
    

    }

    #Use
    copyToDestDir($$PWD/Files/, $$DESTDIR/)


  • Moderators

    @Princein said in how copy the file which in my project directory to the .exe file directory in windows:

    copyToDestDir($$PWD/Files/, $$DESTDIR/)

    try it with

    copyToDestDir($$PWD/Files, $$DESTDIR)


    fixed my post DESTDIR needs the $$ prefix, but if you omit the trailing / it should work. I just tested it.



  • @J.Hilk
    I had tried, but I still not work, and then I changed to 'copyToDestDir($$PWD/Files/, C:\Users\Administrator\Desktop)' ,it still not work too!


  • Moderators

    @Princein said in how copy the file which in my project directory to the .exe file directory in windows:

    'copyToDestDir($$PWD/Files/, C:\Users\Administrator\Desktop)'

    copyToDestDir($$PWD/Files, C:\Users\Administrator\Desktop)



  • @J.Hilk

    I create a two directory in desktop filecopy1 and filecopy2, in the filecopy1 directory, there is a 1.txt file and 2.jpg file,so I change the .pro to

    copyToDestDir( C:\Users\Administrator\Desktop\filecopy1,C:\Users\Administrator\Desktop\filecopy2)

    it still not work


  • Moderators

    @Princein
    use forward slashes or double backwards, the expression doesn't cover simple backwards slashes.



  • @J.Hilk

    #copyToDestDir( C:/Users/Administrator/Desktop/filecopy1,C:/Users/Administrator/Desktop/filecopy2)

    copyToDestDir( C:\Users\Administrator\Desktop\filecopy1,C:\Users\Administrator\Desktop\filecopy2)

    I had tried the '/' and '\' , but now work too, it seem this way has problem, I find a good way, it work for me, and can work on both windows and macos:

    copydata.commands = $(COPY_DIR) C:\Users\Administrator\Desktop\filecopy1 C:\Users\Administrator\Desktop\filecopy2
    first.depends = $(first) copydata
    export(first.depends)
    export(copydata.commands)
    QMAKE_EXTRA_TARGETS += first copydata



  • @Princein said in how copy the file which in my project directory to the .exe file directory in windows:

    replace slashes in destination path for Windows

    win32:dir ~= s,/,,g

    Could it be because you need two backslashes before the 'g' ?
    Edit:
    and dont forget to re-run qmake whenever changing the file.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.