【问题标题】:Exclude multiple file extension in xcopy在 xcopy 中排除多个文件扩展名
【发布时间】:2017-02-22 06:32:09
【问题描述】:

我想从 xcopy 中排除一些扩展。我不知道它们可能存在于哪个文件夹/目录中。

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

xcopy /r /d /i /s /y C:\test1 C:\test2 /exclude:.txt+.exe

对此有帮助吗?

【问题讨论】:

  • xcopy exclude 开关只接受从哪里读取要排除的元素列表的文件。创建文件或使用robocopy/xf开关
  • xcopy/EXCLUDE 选项并没有真正做你想做的事,它不排除带有 .txt.exe 扩展名的文件,它排除了完整路径包含 @ 的所有项目987654329@或.exe在任意位置;例如有一个源文件C:\test1\my.txt.files\file.ext,它也将被排除...
  • 我同意@aschipfl。使用 FOR 命令列出文件并使用命令修饰符检查扩展名是否有效会更安全。

标签: batch-file cmd xcopy


【解决方案1】:

你要找的是这个:

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,它也将被排除。


有几种方法可以克服这个问题,我想向您展示其中的一些:

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

    robocopy "C:\test1" "C:\test2" /S /XO /XF "*.txt" "*.exe"
    
  2. 创建,使用for /F loopxcopy /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.
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 2018-02-07
    • 2020-07-20
    • 2011-05-29
    • 2023-04-08
    相关资源
    最近更新 更多