Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how copy the file which in my project directory to the .exe file directory in windows
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Princein
    wrote on 9 Jan 2019, 08:22 last edited by
    #1

    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!

    J R 2 Replies Last reply 9 Jan 2019, 08:41
    0
    • P Princein
      9 Jan 2019, 08:22

      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!

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 9 Jan 2019, 08:41 last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • P Princein
        9 Jan 2019, 08:22

        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!

        R Offline
        R Offline
        Ratzz
        wrote on 9 Jan 2019, 08:43 last edited by Ratzz 1 Sept 2019, 08:46
        #3

        @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.

        --Alles ist gut.

        1 Reply Last reply
        1
        • J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 9 Jan 2019, 08:53 last edited by
          #4

          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)
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          P 1 Reply Last reply 9 Jan 2019, 09:08
          1
          • J J.Hilk
            9 Jan 2019, 08:53

            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)
            
            P Offline
            P Offline
            Princein
            wrote on 9 Jan 2019, 09:08 last edited by
            #5

            @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/)

            J 1 Reply Last reply 9 Jan 2019, 09:10
            0
            • P Princein
              9 Jan 2019, 09:08

              @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/)

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 9 Jan 2019, 09:10 last edited by J.Hilk 1 Sept 2019, 09:18
              #6

              @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.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              P 1 Reply Last reply 9 Jan 2019, 09:23
              0
              • J J.Hilk
                9 Jan 2019, 09:10

                @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.

                P Offline
                P Offline
                Princein
                wrote on 9 Jan 2019, 09:23 last edited by
                #7

                @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!

                J 1 Reply Last reply 9 Jan 2019, 09:24
                0
                • P Princein
                  9 Jan 2019, 09:23

                  @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!

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 9 Jan 2019, 09:24 last edited by
                  #8

                  @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)


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  P 1 Reply Last reply 9 Jan 2019, 09:33
                  0
                  • J J.Hilk
                    9 Jan 2019, 09:24

                    @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)

                    P Offline
                    P Offline
                    Princein
                    wrote on 9 Jan 2019, 09:33 last edited by
                    #9

                    @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

                    J 1 Reply Last reply 9 Jan 2019, 09:41
                    0
                    • P Princein
                      9 Jan 2019, 09:33

                      @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

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 9 Jan 2019, 09:41 last edited by
                      #10

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


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      P 1 Reply Last reply 9 Jan 2019, 10:00
                      0
                      • J J.Hilk
                        9 Jan 2019, 09:41

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

                        P Offline
                        P Offline
                        Princein
                        wrote on 9 Jan 2019, 10:00 last edited by
                        #11

                        @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

                        1 Reply Last reply
                        0
                        • mranger90M Offline
                          mranger90M Offline
                          mranger90
                          wrote on 9 Jan 2019, 12:59 last edited by mranger90 1 Sept 2019, 13:01
                          #12

                          @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.

                          1 Reply Last reply
                          0

                          1/12

                          9 Jan 2019, 08:22

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved