如何批量复制文件夹中没有其他文件夹的文件?

问题描述 投票:1回答:2

我有以下文件夹结构:

Folder1
  -Folder2
  -File1
  -File2
  -File3

我在批处理脚本中尝试了以下内容来复制Folder1中的所有内容

echo d | xcopy "Folder1\*.*" "DestinationFolder"/f /s /y /r

但我只需要复制File1,File2,File3。需要忽略Folder2。我对如何实现这一目标一无所知。请帮我解决这个问题。

batch-file cmd
2个回答
0
投票

如果Folder2为空,/S会这样做, 但由于你的命令中有/s,所以我猜它不是空的。

所以使用这个:

echo \Folder2\>__tmp4Exclude__
echo d | xcopy "Folder1\*.*" "DestinationFolder" /f /s /y /r /EXCLUDE:__tmp4Exclude__
del __tmp4Exclude__

__tmp4Exclude__是一个临时文件,用于包含复制时要排除的文件列表。

来自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.

0
投票

没有必要使用xcopy来复制文件! xcpoy通常用于复制目录树。

所以,改用copy。你可以做:

copy "Folder1\*.*" "Destination\"

可能你可能需要使用/(-)Y选项:

/Y禁止提示您确认是否要覆盖现有目标文件。 /-Y导致提示您确认要覆盖现有目标文件。

来自copy /?copy帮助页面)

© www.soinside.com 2019 - 2024. All rights reserved.