Simple check - If a files exists in one folder, then delete them from another folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bcvd
Posts: 4
Joined: 24 Feb 2015 13:35

Simple check - If a files exists in one folder, then delete them from another folder

#1 Post by bcvd » 25 Jan 2017 11:26

Hi all. I've tried many variations to get a seemingly simple task to work, but have not had success.

Very simply, for each file in a folder (C:\Pers\test1), I would like to check to see if the file exists in another folder (C:\Pers\test2). If the file exists in test2, then delete it from test1.

Here's my latest attempt after poking around here and on other forums. It does not seem to be doing anything.

Code: Select all

@Echo off
SET LOGFILE=C:\Pers\deletedfiles.log
echo %date% %time% >> %LOGFILE%
call :Tolog >> %LOGFILE%
exit /b 0


:Tolog
setlocal

echo Deleting the following files...
SET orgfolder = C:\Pers\test1
SET newfolder = C:\Pers\test2
cd /d "%orgfolder%"

FOR %%a in (*.*) DO(
IF EXIST "%newfolder%\%%~nxa" (
del %orgfolder%\%%~nxa
echo %%~nxa
) else (
echo MISSING %%~nxa
)
)



Thanks in advance for your assistance.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Simple check - If a files exists in one folder, then delete them from another folder

#2 Post by Squashman » 25 Jan 2017 11:44

Remove the spaces from your SET commands.

bcvd
Posts: 4
Joined: 24 Feb 2015 13:35

Re: Simple check - If a files exists in one folder, then delete them from another folder

#3 Post by bcvd » 25 Jan 2017 14:23

Thank you, Squashman. Much appreciated!

Also, I was missing a space between DO and (
:?

Post Reply