Remove duplicate file. Add filesize test.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Locked
Message
Author
budhax
Posts: 59
Joined: 09 Oct 2006 12:25

Remove duplicate file. Add filesize test.

#1 Post by budhax » 03 May 2007 13:02

Hello,
Here is a script that remove all files in Folder1 if this file exists in Folder2.
So, it does something like: Folder1 - Folder2
in other words, it removes duplicate files.
This script test only the filename: remove the file in Folder1 if the same filename is present in Folder2.
I would to improve it, adding a size file test. remove the file only filesize of both files are the same.
Do you have an idea ?
My line ECHO %%~zf cannot display the file size !!!

F1-F2.bat

Code: Select all

SET Fold1=.\Folder1
SET Fold2=.\Folder2
FOR /F "Delims=" %%f in ('DIR  /b /on "%Fold2%\*.*"') DO (
   IF EXIST "%Fold1%\%%f" DEL /F /Q "%Fold1%\%%f"
   REM ECHO %%~nf  ---  %%~zf
)


Put the script next Folder1 and Folder2.
Thanks

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 10 May 2007 22:12

budax,
For a precise file compare you can use the FC command and process the result (success or fail) to invoke the delete like this:

Code: Select all

SET Fold1=.\Folder1
SET Fold2=.\Folder2
FOR /F "Delims=" %%f in ('DIR  /b /on "%Fold2%\*.*"') DO (
   FC /b "%Fold1%\%%f" "%Fold2%\%%f">NUL&&(
      echo.DEL /F /Q "%Fold1%\%%f"
   )
)


Hope it DOS help :D

Locked