how copy the file which in my project directory to the .exe file directory in windows
-
wrote on 9 Jan 2019, 08:22 last edited by
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.txtINSTALLS += 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!
-
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.txtINSTALLS += 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!
@Princein You can simply create a resource file and put your txt file there. See http://doc.qt.io/qt-5/resources.html
-
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.txtINSTALLS += 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!
wrote on 9 Jan 2019, 08:43 last edited by Ratzz 1 Sept 2019, 08:46Is this what you are looking for https://stackoverflow.com/a/36438513?
Another method here .
Alternatively you can usecopy
http://doc.qt.io/qt-5/qfile.html#copy-1 if file does not exists else you need to check ifexists
andremove
them and then copy. -
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)
-
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)
wrote on 9 Jan 2019, 09:08 last edited byI 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,/,\,gfor(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/) -
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,/,\,gfor(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/)@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.
-
@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!@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)
-
@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)
wrote on 9 Jan 2019, 09:33 last edited byI 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
-
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
@Princein
use forward slashes or double backwards, the expression doesn't cover simple backwards slashes. -
@Princein
use forward slashes or double backwards, the expression doesn't cover simple backwards slashes.wrote on 9 Jan 2019, 10:00 last edited by#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 -
wrote on 9 Jan 2019, 12:59 last edited by mranger90 1 Sept 2019, 13:01
@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/12