xcopy supports * wildcards and even allows you to clone whole directory structure. My project uses these Qt libraries I need to distribute:
Qt5CLucene.dll
Qt5Core.dll
Qt5Gui.dll
Qt5Help.dll
Qt5Multimedia.dll
Qt5Network.dll
Qt5PrintSupport.dll
Qt5Sql.dll
Qt5Svg.dll
Qt5Widgets.dll
Qt5Xml.dll
Qt5XmlPatterns.dll
They are not all in the same directory in the Qt installation and the list is subject to change - especially additions. So I wanted to use wildcard /*/ to find a file anywhere in directory tree:
C:\Qt\5.3.0-64> xcopy ".\*\%NAME%.dll" "%~dp0\release"
It doesn't work, files are not being found. This is the full code:
C:
cd C:\Qt\5.3.0-64\
For %%a in (
"Qt5CLucene"
"Qt5Core"
"Qt5Gui"
"Qt5Help"
"Qt5Multimedia"
"Qt5Network"
"Qt5PrintSupport"
"Qt5Sql"
"Qt5Svg"
"Qt5Widgets"
"Qt5Xml"
"Qt5XmlPatterns"
) do (
xcopy ".\**\%%~ad.dll" "%~dp0\debug"
)
So can I somehow avoid typping full paths (eg. qtbase\bin\Qt5CLucene) in the batch?
try like this (xcopy could ask if you want to create directory - copy is used instead):
For /r "C:\Qt\5.3.0-64\" %%a in (
"*Qt5CLucene.dll"
"*Qt5Core.dll"
"*Qt5Gui.dll"
"*Qt5Help.dll"
"*Qt5Multimedia.dll"
"*Qt5Network.dll"
"*Qt5PrintSupport.dll"
"*Qt5Sql.dll"
"*Qt5Svg.dll"
"*Qt5Widgets.dll"
"*Qt5Xml.dll"
"*Qt5XmlPatterns.dll"
) do (
copy /y "%~fa" "%~dp0\debug"
)
If I understood you correctly, you want to copy all *.dll files from C:\Qt\5.3.0-64\ -folder and it's sub folders to a debug sub folder of your script file folder.
You could try something like this:
:: Start of your *.bat or *.cmd file
FOR /R C:\Qt\5.3.0-64\ %%a IN (*.dll) DO (
xcopy "%%~a" "%dp~0debug\"
)
:: End of your *.bat or *.cmd file
Related
I have a folder structure like:
-MainDir
-f0
-f0
-f0
-a.txt
-b.txt
-c.txt
-f1
-f1
-f1
-aa.txt
-bb.txt
-cc.txt
If you see above my files are in 3rd folder in terms of hierarchy. Now my requirement is to be able to copy only the main folder and it's files to another location. For example: output for above structure should be as following:
-MainDir
-f0
-a.txt
-b.txt
-c.txt
-f1
-aa.txt
-bb.txt
-cc.txt
but when I try using XCOPY, all the folders get copied as it is. So I am trying to figure out a way to achieve my target folder structure as shown above
The following will loop the directories under main, find all files, and put them all in the top most sub directory in destination as you're looking for:
CMD Script:
#(SETLOCAL
ECHO OFF
SET "_MainDir=C:\Main\Dir"
SET "_DestDir=%Temp%\Destination\Main\Dir"
)
FOR /D %%A in (
%_MainDir%\*
) DO (
IF NOT EXIST "%_DestDir%\%%~nxA" MD "%_DestDir%\%%~nxA"
FOR /F %%a IN ('
DIR /A-D /S /B "%%~fA\*"
') DO (
ECHO Copying: "%%~fa"
ECHO To: "%_DestDir%\%%~nxA\%%~nxa"
COPY /B /V /Y "%%~fa" "%_DestDir%\%%~nxA\%%~nxa"
)
)
This is a quite easy task using xcopy together with a for /D loop, given that the three sub-directories in each hierarchy branch are equally named (like f0\f0\f0 and f1\f1\f1):
rem // Loop through immediate sub-sirectories of main directory:
for /D %%I in ("D:\MainDir\*") do (
rem /* Build path to files into second sub-directory (though there are no files);
rem the third sub-directory and its contents is regarded due to the `/S` option;
rem the copy destination directory then receives the whole contents of the
rem second source sub-directory, including the third source sub-directory: */
xcopy /S /I "%%~I\%%~nxI\*.*" "D:\CopyDir"
)
I need to do something with a batch file....
I need to copy a folder to another folder but...
If the new folder exist, I need to verify if the file in new folder exist, then I need to rename the file with «.old» at the end of this file before to copy the new file.
I have a great experience of programming in Java, php etc, but not really with batch file...
I m using a syntax of java/php to explain my problem....
set folderOrigin=d:\test1
set folderFinal=d:\test5
if EXIST %folderFinal% (
for (fileOrigin : folderOrigin){
variableNamefileOrigin = fileOrigin
for (fileFinal : folderFinal){
variableNamefileFinal = fileFinal
if (variableNamefileOrigin == variableNamefileFinal){
newvariable = variableNamefileFinal + ".old"
ren variableNamefileFinal newvariable
xcopy /s /q %folderOrigin%+%variableNamefileOrigin%
%folderFinal%+%variableNamefileFinal%
}
}
}
) else (
xcopy /s /q %dossierOrigine% %dossierDestinataire%
)
pause
Thx everyone !
Assuming that the source directory ("folders" are artifacts in the GUI; the structures in the filesystem are "directories") is %sourceFolder% and the destination directory is %finalFolder%; and also assuming that you need to copy only files (not an entire subtree):
To loop through the files in %sourceFolder% you use a for loop:
for %%f in ("%sourceFolder%\*") do call :copyOneFile "%%~f"
exit /b
In the subroutine :copyOneFile you have the current file as %1. To check whether it exists in %finalFolder% you use if exist, and if so you rename it, but not before checking if the .old file exists already:
:copyOneFile
if exist "%finalFolder%\%~nx1" (
if exist "%finalFolder\%~n1.old" del "%finalFolder\%~n1.old"
ren "%finalFolder\%~nx1" "%~n1.old"
)
Now you can copy the file from the source folder to the destination folder:
copy "%~1" "%finalFolder%
To understand the constructions %~nx1 and so on, use for /?. Note than the second argument of ren must have only the filename, a path is not allowed.
If you need to copy an entire subtree then:
After copying the files, redo with for /d to get directories.
Use the appropriate commands instead of del and copy.
I get stuck on a basic problem :(
I want to read a txt file from a batch file, copy files name like found in the file to a new directory
Exemple :
- My src folder contains Microsoft KB (arround 300 files) with name Like "Windows 6.1-KBxxxxx.msu"
- My file [KB.Txt] is look like :
KB320000
KB320001
- dst folder is empty
...
So I want to copy only KB in my KB.txt from src to dst
My batch look like :
set src=%~dp0\src
set dst=%~dp0\dst
set file=%~dp0KB.txt
for /f "delims=" %%i in (%file%) do (
xcopy "%src%\%%i" "%dst%\%%i" /i /z /y /s
)
This do not work because the script only try to copy with exact name.
I have tested with wildcards with no success... What I have done wrong ?
Thanks!
set src=%~dp0\src
set dst=%~dp0\dst
set file=%~dp0KB.txt
for /f "delims=" %%i in (%file%) do (
copy /y "%src%\*%%i*" "%dst%\"
)
copy command supports wildcards so enclosing %%i with asterisks should work.
Using XCOPY for files (the command is designed for folders) is a bad idea as it will prompt if you want to create a destination directory.
I am very new to batch scripting.
I need to write a batch script to search for a specific file(usually .zip or .7z extension) located on network drive directory(containing multiple folder and sub-folders with space in name) and copy the same to my local drive.
Also I need to copy a zip file containing "elf" keyword which will also be located in the same directory where my file is present.
For example:
Search file: abc.zip
Network drive:\abc.com\test
So basically I need to search for my file abc.zip in the network directory(including sub-folders) and if found copy the same to my local drive(say c:\Temp) and also I need to copy *elf* file to the same local directory.
Thanks in Advance.
#echo off
rem Prepare environment
setlocal enableextensions
rem Configure paths
set "source=\abc.com\test"
set "target=c:\test"
rem Change drive/path to source of files
pushd "%source%"
rem Recurse folders searching adecuated files and when found, copy to target
for /f "tokens=*" %%f in ('dir /s /b "abc.*z*" ^| findstr /i /e /c:".zip" /c:".7z"') do (
copy /y "%%~ff" "%target%"
if exist "%%~dpf\*elf*.zip" copy /y "%%~dpf\*elf*.zip" "%target%"
)
rem Return to previous drive/path
popd
rem Clean
endlocal
This will search the source folder hierarchy for indicated filename and an aproximate match based on file extension (.*z* matchs .zip, .7z and more), filters file extensions to only accept the cases needed (.zip and .7z) and copy the required files to target folder.
Have you considered using the Extended Copy Utility (xcopy)? Syntax is as simple as
xcopy "<directoryToCopyFrom>" "<directoryToCopyTo>" /C /S /D /Y /I
This will work assuming you're wanting to write a Windows batch script.
Searching for the "elf" string will be a bit trickier though. You might consider doing that using Java, and then call the batch file from the Java program.
for /d /r "drive:\abc.com\test" %%A in (*) do (
if exist "%%~A\abc.zip" copy "%%~A\abc.zip" "C:\Temp"
if exist "%%~A\*elf*" copy "%%~A\*elf*" "C:\Temp"
)
I am trying to find a way to create a Windows batch script that will look at a target folder full of .pdf files and move (or copy) them to another directory with existing subfolders based on the filename.
The files and folders are names of actual people. I want to be able to get that person's pdf into their existing folder using a script.
Say I have 2 files in my folder; smithJohn015.pdf and thomasBill030.pdf.
I would like to be able to put smithJohn015.pdf into folder SmithJohn and thomasBill030.pdf into folder ThomasBill.
I don't want the script to create new folders or overwrite existing files if there's a duplicate filename.
I'm not necessarily looking for anyone to write a script for me, but if anyone can just get me started in the right direction it would be appreciated.
Try modifying this answer for your evil purposes.
#echo off
setlocal
pushd "c:\path\to\PDFs"
for /d %%I in (c:\Path\To\People\*) do (
for %%F in (*) do (
for /f %%A in ('echo %%~nF ^| find /i "%%~nI"') do (
set /p a="Moving %%F to %%I... "<NUL
move "%%F" "%%I" >NUL
echo Done.
)
)
)
popd
You'll need to add a check for if not exist pdffile before the move, but there's a starting direction for you anyway.
The following assumes the target subfolders' location contains only the subfolders where the PDFs may go and that every PDF that you want to move has a name formatted as the corresponding subfolder's name followed by exactly three characters (same as in your examples):
#ECHO OFF
FOR /D %%D IN ("D:\path\to\subfolders\*") DO (
MOVE "D:\path\to\PDFs\%%~nD???.pdf" "%%D"
)
Or as a one-liner to execute directly at the command prompt:
FOR /D %D IN ("D:\path\to\subfolders\*") DO (MOVE "D:\path\to\PDFs\%~nD???.pdf" "%D")
folderlist.txt contains all names of folders in which you want to copy respective PDFs.
xcopy is copy command. format xcopy "source" "destination" /parameters.
pause is just to keep command window on
for /F "tokens=*" %%A in (folderlist.txt) do (
xcopy "E:\path\to\source folder\<prefix>%%A<suffix>.pdf" "E:\path\to\destination folder\<prefix>%%A<suffix>\" /s)
pause
You can use wildcards in source & destination paths.