排除for循环搜索中的目录 [英] Excluding a directory in for loop's search
问题描述
我有以下代码:
FOR /R D:\1_FOLDER %f IN (*.jpg,*.png) DO XCOPY %f D:\2_FOLDER /H /C
我有错误
参数的数目无效
Invalid numbers of parametres
目录1_FOLDER中的第二个问题还有2个文件夹X_FOLDER和Y_FOLDER,我想for循环只在X_FOLDER中搜索,并且只复制X_FOLDER的文件。
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.
我有第二个问题,当我想从C:\复制,我想从for循环搜索排除Windows文件夹。
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.
推荐答案
当您指定包含 SPACE 字符的不引用路径时,错误消息来自 xcopy
(或其他标记分隔符,如,
,;
, =
, TAB 或非中断空格(ASCII 0xFF
))。
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
)).
实际上我没有看到任何理由使用为/ R
循环的手头的任务,因为 xcopy
/ EXCLUDE
选项。以下是在输入 xcopy /?
时出现的帮助测试的摘录:
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.
所以在你的例子中,创建一个文本文件 exclusions.lst
)具有以下内容:
So for your example, create a text file (let us call it exclusions.lst
) with the following content:
D:\1_FOLDER\Y_FOLDER\
或者,如果您想要每个子目录在任何要排除的位置调用 Y_FOLDER
,写入:
Alternatively, if you want every subdirectory called Y_FOLDER
in any location to be excluded, write:
\Y_FOLDER\
由于您有多个文件模式( *。jpg
和
循环中使用 *。png
),您可以为每个命令提供 xcopy
命令行(使用适当的开关执行递归,而不是在/ R 循环):
xcopy
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"
或者你可以在另一个文本文件中列出文件模式(让我们称之为 patterns.lst
):
Or you could list the file patterns in another text file (let us call it patterns.lst
):
*.jpg
*.png
然后你可以使用 for / F
该文件并将每个模式/行输入 xcopy
命令:
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"
)
不要忘记更换每个 cmd
)中的<%% 通过%
Do not forget to replace each %%
by %
if you want to try this in command prompt (cmd
) directly.
两个文本文件 exclusions.lst
和 patterns.lst
预期在当前工作目录中。
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.
如果您需要用于指定过滤器以排除或包含某些文件的高级选项,您可能会对 robocopy
命令。
If you need advanced options for specifying filters to exclude or include certain files, you might be interested in the robocopy
command.
这篇关于排除for循环搜索中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!