开发者

How can I write a batch file that will delete all the files who exist in another directory?

I want to write a batch file that will remove the file overlap between two folders.

In oth开发者_StackOverflow中文版er words, I want it to say "Remove from folder A all files which also appear in folder B".

From a SQL point of view, it would be something like

DELETE FROM FOLDERA WHERE EXISTS(SELECT FILE FROM FOLDERB)

but I want to accomplish that in the windows filesystem.


Answering my own question:

set if_it_exists_here=C:\folderB
set then_delete_it_there=C:\folderA
cd "%if_it_exists_here%"
FOR /F "tokens=*" %%G IN ('dir *.* /b /a:-d') DO del "%then_delete_it_there%\%%G"


SET if_it_exists_here=C:\folderB
SET then_delete_it_there=C:\folderA
CD "%if_it_exists_here%"
FOR %%A IN (*.*) DO (
 IF EXIST "%then_delete_it_there%\%%~nxA" DEL /Q /F %then_delete_it_there%\%%~nxA
)


Building on the answer by Mechaflash, here is a version that works with UNC paths.

SET if_it_exists_here=\\server1\some\folder
SET then_delete_it_there=\\server2\some\folder
CD "%if_it_exists_here%"
FOR %%A IN ("%if_it_exists_here%\*.*") DO (
 IF EXIST "%then_delete_it_there%\%%~nxA" DEL /Q /F %then_delete_it_there%\%%~nxA
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜