00 Votes

Batch Script: Delete Files from Folder not in another Folder

Question by Guest | 2023-06-26 at 22:45

I found this on the web but it does the opposite of what it says. It deletes files that match. Can anyone help?

I use the following to compare files within different folders and delete the ones from first folder which does not exists on second one.

echo off
set "Folder1=C:\Documents and Settings\Administrator\My Documents\My Pictures"
set "Folder2=C:\Documents and Settings\Administrator\My Documents\My eBooks"

for /f "delims=" %%F in ('dir /b "%folder2%"') do (
if not exist "%folder1%\%%F" (
fc /b "%folder1%\%%F" "%folder2%\%%F"
if "%errorlevel%" EQU "1" (
  del "%folder1%\%%F" && echo Deleted "%%F"
) else (
  del "%folder1%\%%F" && echo Deleted "%%F"
pause
Exit

ReplyPositiveNegative
00 Votes

Are you sure this is the full script? There are some brackets missing.

And why are the two lines in the last if condition the same?

What if you change "if not exist" to "if exist"?
2023-06-27 at 03:20

ReplyPositive Negative
Reply

Related Topics

Rename File to its Folder Name

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.