Batch script to move files from one location to another except one file

时间:2018-02-01 18:40:34

标签: batch-file

I am writing a batch script to move files from one location to another.Which works fine.But when I want to exclude one file from it, there comes the issue.

I have the following files in folder XYZ:

ABC_2017.qvd
ABC_201701.qvd
ABC_201702.qvd
ABC_201703.qvd
ABC_201704.qvd
ABC_201705.qvd
ABC_201706.qvd

I want to move all the files from this XYZ folder from input path to a folder WXY in Output path.(I have created variables for the paths)

move %Input_Path%\XYZ\ABC_2017*.qvd %Output_Path%\WXY

Doing this is moving all the files including ABC_2017.qvd. But I want to exclude ABC_2017.qvd from moving.

I tried this but it does not work

move %Input_Path%\XYZ\ABC_2017*.qvd %Output_Path%\WXY /exclude:%Input_Path%\XYZ\ABC_2017.qvd 

What can I do?

1 个答案:

答案 0 :(得分:0)

使用ROBOCOPY作为@Squashman说:

ROBOCOPY "%Input_Path%\XYZ" "%Output_Path%\WXY" /MOV /XF ABC_2017.qvd

这也应该有效:

FOR %%G IN ("%Input_Path%\XYZ") DO (
    IF NOT "%%~nxG"=="ABC_2017.qvd" MOVE "%%G" "%Output_Path%\WXY"
)

如果文件的名称和扩展名不等于ABC_2017.qvd,则会移动每个文件。