Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

cmd - Exclude multiple file extension in 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:est1 C:est2 /exclude:.txt+.exe

Anh help on this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you are looking for is this:

xcopy /R /D /I /S /Y /EXCLUDE:exclude.txt "C:est1" "C:est2"

Together with the following content of exclude.txt:

.txt
.exe

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:est1my.txt.filesfile.ext, it is going to be excluded also.


There are several methods to overcome this, some of which I want to show you:

  1. Use the robocopy command and its /XF option:

    robocopy "C:est1" "C:est2" /S /XO /XF "*.txt" "*.exe"
    
  2. Create a , 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:est1" ^&^& xcopy /L /R /D /I /S /Y "." "C:est2" ^| 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:est2\%%~F.."
            rem // Actually copy file, hide summary line:
            > nul copy /Y "C:est1\%%~F" "C:est2\%%~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.
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...