在xcopy中排除多个文件扩展名

use*_*712 -1 cmd xcopy batch-file

我有几个扩展,我想从xcopy中排除.我不知道那些文件夹/目录可能存在.

以下是我正在使用的命令:

xcopy /r /d /i /s /y C:\test1 C:\test2 /exclude:.txt+.exe
Run Code Online (Sandbox Code Playgroud)

Anh对此有所帮助?

asc*_*pfl 9

你在寻找的是:

xcopy /R /D /I /S /Y /EXCLUDE:exclude.txt "C:\test1" "C:\test2"
Run Code Online (Sandbox Code Playgroud)

连同以下内容exclude.txt:

.txt
.exe
Run Code Online (Sandbox Code Playgroud)

但是,/EXCLUDE选择xcopy非常差.它并不真正排除具有给定扩展名的文件,它实际上排除了其完整路径包含.txt.exe位于任何位置的所有项目.假设有一个源文件C:\test1\my.txt.files\file.ext,它也将被排除.


有几种方法可以解决这个问题,其中一些我想告诉你:

  1. 使用robocopy命令及其/XF选项:

    robocopy "C:\test1" "C:\test2" /S /XO /XF "*.txt" "*.exe"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用循环创建,以及预过滤文件:for /Fxcopy /L

    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.
    
    Run Code Online (Sandbox Code Playgroud)