I have the following line of code:
FOR /R D:\1_FOLDER %f IN (*.jpg,*.png) DO XCOPY %f D:\2_FOLDER /H /C
The first problem what i have, the command doesn't run, i get the error
Invalid numbers of parametres
The second problem in directory 1_FOLDER i have 2 more folders X_FOLDER and Y_FOLDER, i want the for loop to search only in X_FOLDER, and copy only files of the X_FOLDER.
I have the second problem same when i want to copy from C:\ , i want to exclude the Windows folder from for loop's search.
The error message comes from xcopy
when you specify unquoted paths containing SPACE characters (or other token separators like ,
, ;
, =
, TAB or non-break spaces (ASCII 0xFF
)).
Anyway, actually I do not see any reason to use a for /R
loop for the task at hand, because xcopy
features an /EXCLUDE
option. Here is an excerpt of the help test appearing when typing xcopy /?
:
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.
So for your example, create a text file (let us call it exclusions.lst
) with the following content:
D:\1_FOLDER\Y_FOLDER\
Alternatively, if you want every subdirectory called Y_FOLDER
in any location to be excluded, write:
\Y_FOLDER\
Since you have multiple file patterns (*.jpg
and *.png
) to be processed, you could provide an xcopy
command line for each of them (let xcopy
do the recursion by using the appropriate switches, rather than wrapping a for /R
loop around):
xcopy /C /H /K /S /I /EXCLUDE:exclusions.lst "D:\1_FOLDER\*.jpg" "D:\2_FOLDER"
xcopy /C /H /K /S /I /EXCLUDE:exclusions.lst "D:\1_FOLDER\*.png" "D:\2_FOLDER"
Or you could list the file patterns in another text file (let us call it patterns.lst
):
*.jpg
*.png
Then you could use a for /F
loop to read that file and feed each pattern/line to an xcopy
command:
for /F "eol=| delims=" %%P in (patterns.lst) do (
xcopy /C /H /K /S /I /EXCLUDE:exclusions.lst "D:\1_FOLDER\%%~nxP" "D:\2_FOLDER"
)
Do not forget to replace each %%
by %
if you want to try this in command prompt (cmd
) directly.
Both text files exclusions.lst
and patterns.lst
are expected to be in the current working directory.
Note that all methods shown here do not copy any empty directories from the source.
If you need advanced options for specifying filters to exclude or include certain files, you might be interested in the robocopy
command.