Steve Over
Steve Over

Reputation: 21

DOS BATCH: How to compare the existence of 2 files with the same name in 2 separate folders?

I want to check all the files in one folder to see if they exist in another folder. When a file is found in the first folder that doesn't exist in the second folder I want that file to be deleted.

Is this possible?

Upvotes: 2

Views: 333

Answers (1)

dbenham
dbenham

Reputation: 130829

Edit path info as appropriate (or incorprate batch arguments %1 %2), and remove ECHO that precedes DEL once you confirm you are getting the correct results.

@echo off
setlocal
set "dir1=."
set "dir2=d1"
set tempFile="%temp%\exclude%random%.txt"
dir /b "%dir2%" >%tempFile%
for /f "eol=: delims=" %%F in ('dir /b /a-d "%dir1%" ^| findstr /vixg:%tempFile%') do echo del "%dir1%\%%F"
del %tempFile%

Note - This solution is simply comparing names. Two completely different files will be considered the same if they have the same name.

Upvotes: 2

Related Questions