Windows Batch commands to copy multiple file to multiple subdirectories - batch-file

ok so I'm on Windows... hmm 7 still
I tried to find how to copy files from one directory to multiple, but I only found how to copy from various into one or one file only to various. I need to copy various file to various directories in function of the file name.
So I have this group of documents, I have a beautiful excel list which indicate for which document where I should send them so I tried a quick concatenate to have a batch file
The command line open well but it repeat the first line ad libitum
Can you explain to me where is my error ?
xcopy "C:\Users\me\Documents\Test\64340C-P3-4J01001A-PCS430033-01-Rev1.pdf" "C:\Users\me\Documents\Test\PE3-L-S04-00002" /z /i
xcopy "C:\Users\me\Documents\Test\64340C-P3-4J01001B-PCD430004-01-Rev1.pdf" "C:\Users\me\Documents\Test\PE3-L-S04-00013" /z /i
xcopy "C:\Users\me\Documents\Test\64340C-P3-4J01001B-PCD430004-03-Rev1.pdf" "C:\Users\me\Documents\Test\PE3-L-S04-00013" /z /i
xcopy "C:\Users\me\Documents\Test\64340C-P3-4J01001B-PCD430005-01-Rev1.pdf" "C:\Users\me\Documents\Test\PE3-L-S04-00013" /z /i
or
xcopy 64340C-P3-4J01001A-PCS430033-01-Rev1.pdf \PE3-L-S04-00002 /z /i
or
xcopy C:\Users\me\Documents\Test\64340C-P3-4J01001A-PCS430033-01-Rev1.pdf c:\Users\me\Documents\Test\PE3-L-S04-00002 /z /i

Related

Ignore one folder and its contents in batch operation

I've tried to create a batch file that will remove all files and sub-directories from a folder. The command I found that worked is this
FORFILES /p “X:\DAILY\1 MONDAY” /m *.* /c “cmd /c Del /F /Q #path” /d -7 /s & FORFILES /p “X:\DAILY\1 MONDAY” /S /D -7 /C “cmd /c IF #isdir == TRUE rd /S /Q #path”
Now my users tell me there is a Reference folder under 1 MONDAY that they don't want purged (but all other subdirectories should be emptied and deleted.) Can anyone advise how I might accomplish that?
Thank you!
for /f %%a in ("X:\Daily\1 MONDAY\*") do (if %%a neq "<filename you want to save>" (del %%a))
NOT YET TESTED
This should work though. Put the files you want to save in <filename you want to save>
It is extremely inefficient to use FORFILES for such a task because FORFILES has to start cmd.exe for each file found to delete the file older than seven days and for each folder to remove.
A much better solution would be:
#echo off
%SystemRoot%\System32\robocopy.exe "X:\DAILY\1 MONDAY" "X:\DAILY\1 MONDAY\WeeklyDelete" /E /XD "X:\DAILY\1 MONDAY\WeeklyDelete" "X:\DAILY\1 MONDAY\Reference" /MINAGE:7 /MOVE /NDL /NFL /NJH /NJS
if exist "X:\DAILY\1 MONDAY\WeeklyDelete\" rd /Q /S "X:\DAILY\1 MONDAY\WeeklyDelete"
ROBOCOPY searches
for all files in the directory X:\DAILY\1 MONDAY
and its subdirectories including empty directories because of option /E with
excluding the files in the two directories X:\DAILY\1 MONDAY\WeeklyDelete and X:\DAILY\1 MONDAY\Reference and all their subdirectories because of option /XD "X:\DAILY\1 MONDAY\WeeklyDelete" "X:\DAILY\1 MONDAY\Reference" with
excluding all files last modified in last seven days because of option /MINAGE:7.
The found files and folders matching theses criteria are moved to X:\DAILY\1 MONDAY\WeeklyDelete whereby the destination folder is automatically created on not already existing.
The options /NDL /NFL /NJH /NJS are for not printing the list of moved directories, list of moved files, the header and the summary.
A folder is moved only if being empty after moving all files matched by these criteria. So a folder is not moved on containing a file or subfolder.
The option /S instead of /E and the option /MOV instead of /MOVE can be used to move only files and do not move also folders being empty before or after moving the files.
The file and folder movements are done very fast by ROBOCOPY because of destination folder is on same drive as source folder which means just the file system of this drive must be updated and no file data must be moved at all.
An IF condition is used after ROBOCOPY finished with updating the file system to move the files and folders to verify if the destination folder X:\DAILY\1 MONDAY\WeeklyDelete exists. In this case command RD is used to delete this folder quietly because of option /Q and with all files and subdirectories because of option /S. This action is again just a file system update not really deleting file data stored on storage media and so is processed very fast.
Open a command prompt window and execute there the following commands to read more about the used commands in the three lines above.
echo /?
if /?
rd /?
robocopy /?
The commands are described also
by Microsoft with the documentations for the Windows Commands and
by SS64.com with A-Z index of Windows CMD commands and on many other websites.
Based upon your provided information, I would suggest this as your updated batch file:
#%__AppDir__%forfiles.exe /P "X:\DAILY\1 MONDAY" /C "cmd /C If #IsDir == FALSE 0x28Del /A /F #Path0x29 Else If /I Not #File == 0x22Reference0x22 RD /S /Q #Path" /D -7
That's it, nothing else to be added or modified.

How to search for folders with specific naming convention and copy them to different location along with its contents?

I have a directory F:\Input with various subfolders in it.
I need to specifically copy only those folders which has same naming convention such as India012345.zip, India09876.zip, etc. These are really folders with unusual .zip in folder name and not ZIP files!
I need to copy those folders along with their contents (subfolders and files) to a different location.
CD /D F:\Input\
FOR /f "tokens=*" %%I in ('dir /s /b /a:d "India*"') do echo|xcopy "%%I" "E:\output\" /s
This is my present code which copies the contents but not the main folders India012345.zip.
Folder structure of F:\Input:
Country
state
0021
op 1
qwerty
India09876.zip (folder, not compressed)
31-Jun-2016
XML and PDF files
vcxz
03-Aug-2016.zip (file, compressed)
0031
op 1
India1234.zip (folder, not compressed)
31-Jun-2016
XML and PDF files
vcxz
04-Aug-2016.zip (file, compressed)
India*.zip will be unique in terms of nos.
It is not possible to rename. Please provide solution to copy as follows:
Folder E:\Output should contain after batch execution:
India09876.zip
31-Jun-2016
XML and PDF files
vcxz
03-Aug-2016.zip
India1234.zip
31-Jun-2016
XML and PDF files
vcxz
04-Aug-2016.zip
The India*.zip directory name must be also specified in destination path and some more options of command XCOPY should be used like /I to automatically create the destination folder, too.
#echo off
for /F "delims=" %%I in ('dir "F:\Input\India*.zip" /A:D /B /S 2^>nul') do xcopy "%%I" "E:\Output\%%~nxI\" /C /H /I /K /Q /R /S /Y >nul
It would be very easy possible to remove unusual .zip from the folder names during the copying process by using:
#echo off
for /F "delims=" %%I in ('dir "F:\Input\India*.zip" /A:D /B /S 2^>nul') do xcopy "%%I" "E:\Output\%%~nI\" /C /H /I /K /Q /R /S /Y >nul
%%~nI is used instead of %%~nxI to get rid of "file extension" from folder name.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
for /?
xcopy /?
And read also the Microsoft article about Using command redirection operators for an explanation of >nul.
2^>nul is 2>nul with redirection operator > escaped with ^ to apply 2>nul on execution of command DIR instead of interpreting the redirection on parsing the FOR command line which would result in an exit of batch processing because of a syntax error.
2>nul used on execution of DIR results in redirecting a possible error message output by DIR to STDERR to device NUL to suppress it. Command DIR outputs the confusing error message File not found to standard error stream if it can't find any folder matching the folder name wildcard pattern India*.zip.

Batch script copy folder using wildcard

I need to copy a folder matching a wildcard, for instance FOLDER_*. That folder will be in the presence of other files, so I need the command to segregate it from everything else. Also, the command needs to recursively search through the directory, and return only the FOLDER that matches the wildcard, with its contents intact. Then it needs to copy it to another folder. Any ideas? I've tried quite a few variants - here is the last thing I tried.
for /D /R %%f in (FOLDER_*) do xcopy %%f %~dp0\TestResults
Next code snippet should copy all found FOLDER_* folders including their subfolders but omits all their parent folder(s) like upfolder\ in upfolder\FOLDER_* (however could be improved to include it, of course).
for /F loop against dir /b /s /ad (a static list of subfolders) is used instead of for /D /R as the option /d /r is undocumented.
Pay your attention to recursion like FOLDER_main\FOLDER_sub1\FOLDER_sub11 etc.
Operational mkdir and xcopy commands are merely echoed for debugging purposes only:
#echo OFF
for /F "delims=" %%f in ('dir /B /S /AD FOLDER_*') do (
echo mkdir "%~dp0\TestResults\%%~nxf" 2>NUL
echo xcopy /S /E /C "%%~ff" "%~dp0\TestResults\%%~nxf\"
)
Consider adding more switches to xcopy, for instance
/H Copies hidden and system files also.
/R Overwrites read-only files.
/Y Suppresses prompting to confirm you want to overwrite an existing destination file.

Copy list of file names in text file

I need to copy a list of files in a text file to a new directory, while preserving the directory structure. My file looks like this:
F326819.B88
F326819.B89
F326819.B90
F326731.B44
F326733.B61
F326733.B62
I need a batch command that will "pick" the ones listed in the text file and copy them over to a new directory, preserving the directory structure. I tried this code but it says invalid number of parameters:
for /f "delims=" %%i in (W:\GasImages\ServiceCards\WindLake.txt) do echo D|xcopy %%i "W:\GasImages\ServiceCards" "D:\Marc\WindLake" /i /z /y /e
Any help would be appreciated.
xcopy takes in a list of source files, followed by a destination directory. When there are multiple directories passed in, it doesn't know what to do with them all.
Try this (note that this code assumes the files to copy are in C:\GasImages\ServiceCards)
#echo off
for /f "delims=" %%I in (C:\GasImages\ServiceCards\WindLake.txt) do (
xcopy "C:\GasImages\ServiceCards\%%I" "D:\Marc\Windlake\" /I /Z /Y /E
)
pause
Also, the echo D| is unnecessary with the /I flag.

Exclude folders in batch-copy-script

I am using a batch file on a USB stick to backup my pictures. I use the following command:
for /r C:\ %%x in (*.jpg *.png *.gif) do #copy /y %%x .
I want to exclude files in the mailfolder WINDOWS and PROGRAM FILES.
Does anyone have an idea how I can do this with a batch file?
Drop COPY and use ROBOCOPY which exists in Windows Vista+ & is downloadable for prior versions.
It supports /XD to exclude specific directories & /XF to exclude file masks at the command line.
E.g.
robocopy.exe c:\ c:\destination\ *.jpg *.png *.gif /xd "Program files" "windows" /S
(Note this will recreate the directory structure in c:\destination\, which thinking about it may not be what you want)
Turn copy into xcopy and then you can use it's /EXCLUDE switch
#xcopy %%x /y /EXCLUDE:\WINDOWS\
See xcopy /? for the details.

Resources