在xcopy中排除多个文件扩展名 [英] Exclude multiple file extension in xcopy
问题描述
我有几个要从xcopy中排除的扩展名.我不知道它们可能在哪个文件夹/目录中.
I have few extensions which I want to exclude from xcopy. I don't know in which folder/directory those may exist.
下面是我正在使用的命令:
Below is the command I'm using:
xcopy /r /d /i /s /y C:\test1 C:\test2 /exclude:.txt+.exe
对此有何帮助?
推荐答案
您正在寻找的是这个
xcopy /R /D /I /S /Y /EXCLUDE:exclude.txt "C:\test1" "C:\test2"
连同exclude.txt
的以下内容:
.txt
.exe
但是,xcopy
的/EXCLUDE
选项非常差.它实际上并没有排除具有给定扩展名的文件,而是实际上排除了其完整路径在任何位置都包含.txt
或.exe
的所有项目.假设有一个源文件C:\test1\my.txt.files\file.ext
,它也将被排除.
However, the /EXCLUDE
option of xcopy
is very poor. It does not really exclude files with the given extensions, it actually excludes all items whose full paths contain .txt
or .exe
at any position. Supposing there is a source file C:\test1\my.txt.files\file.ext
, it is going to be excluded also.
有几种方法可以克服这个问题,其中一些我想向您展示:
There are several methods to overcome this, some of which I want to show you:
-
使用
robocopy
命令及其/XF
选项:
robocopy "C:\test1" "C:\test2" /S /XO /XF "*.txt" "*.exe"
创建一个batch-file ,使用 for /F
循环 xcopy /L
,以预过滤文件:
Create a batch-file, using a for /F
loop, together with xcopy /L
, to pre-filter the files:
set /A "COUNT=0"
for /F "delims=" %%F in ('
rem/ // List files that would be copied without `/L`; remove summary line: ^& ^
cd /D "C:\test1" ^&^& xcopy /L /R /D /I /S /Y "." "C:\test2" ^| find ".\"
') do (
rem // Check file extension of each file:
if /I not "%%~xF"==".txt" if /I not "%~xF"==".exe" (
rem // Create destination directory, hide potential error messages:
2> nul mkdir "C:\test2\%%~F\.."
rem // Actually copy file, hide summary line:
> nul copy /Y "C:\test1\%%~F" "C:\test2\%%~F" && (
rem // Return currently copied file:
set /A "COUNT+=1" & echo(%%~F
) || (
rem // Return message in case an error occurred:
>&2 echo ERROR: could not copy "%%~F"!
)
)
)
echo %COUNT% file^(s^) copied.
这篇关于在xcopy中排除多个文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!