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 (ASCII0xFF
)).Anyway, actually I do not see any reason to use a
for /R
loop for the task at hand, becausexcopy
features an/EXCLUDE
option. Here is an excerpt of the help test appearing when typingxcopy /?
:So for your example, create a text file (let us call it
exclusions.lst
) with the following content:Alternatively, if you want every subdirectory called
Y_FOLDER
in any location to be excluded, write:Since you have multiple file patterns (
*.jpg
and*.png
) to be processed, you could provide anxcopy
command line for each of them (letxcopy
do the recursion by using the appropriate switches, rather than wrapping afor /R
loop around):Or you could list the file patterns in another text file (let us call it
patterns.lst
):Then you could use a
for /F
loop to read that file and feed each pattern/line to anxcopy
command:Do not forget to replace each
%%
by%
if you want to try this in command prompt (cmd
) directly.Both text files
exclusions.lst
andpatterns.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.