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
Forum Updated to NodeBB v4.3 + New Features

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.2k Views 1 Watching
  • 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 Princein

    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!

    RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by Ratzz
    #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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on 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
      1
      • J.HilkJ J.Hilk

        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 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.HilkJ 1 Reply Last reply
        0
        • P Princein

          @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.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #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
          0
          • J.HilkJ J.Hilk

            @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 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.HilkJ 1 Reply Last reply
            0
            • P Princein

              @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.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 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
              0
              • J.HilkJ J.Hilk

                @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 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.HilkJ 1 Reply Last reply
                0
                • P Princein

                  @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.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on 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
                  0
                  • J.HilkJ J.Hilk

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

                    P Offline
                    P Offline
                    Princein
                    wrote on 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 last edited by mranger90
                      #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

                      • Login

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