Delete files if exist in a diffrent one

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stanian
Posts: 7
Joined: 22 Oct 2014 13:10

Delete files if exist in a diffrent one

#1 Post by stanian » 22 Oct 2014 13:17

So i want a batch that delete files if they exist in another folder.
delete file/s from "desktop" if the files exist in "f:/lnkremove"
Big thanks if somone can help!

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

Re: Delete files if exist in a diffrent one

#2 Post by Squashman » 22 Oct 2014 13:47

remove the word ECHO if you are satisfied with the results it outputs to the screen.

Code: Select all

@echo off
for %%G in ("f:\lnkremove\*") do IF EXIST "%userprofile%\Desktop\%%~nxG" ECHO del "%userprofile%\Desktop\%%~nxG"

stanian
Posts: 7
Joined: 22 Oct 2014 13:10

Re: Delete files if exist in a diffrent one

#3 Post by stanian » 22 Oct 2014 13:59

Tankyou so much for this fast answer. Have waited stackoverflow for this
Tanks again!

stanian
Posts: 7
Joined: 22 Oct 2014 13:10

Re: Delete files if exist in a diffrent one

#4 Post by stanian » 25 Oct 2014 09:26

Squashman wrote:remove the word ECHO if you are satisfied with the results it outputs to the screen.

Code: Select all

@echo off
for %%G in ("f:\lnkremove\*") do IF EXIST "%userprofile%\Desktop\%%~nxG" ECHO del "%userprofile%\Desktop\%%~nxG"



I have tested this now. but cant get it to work.

Code: Select all

for %%G in ("%~d0\cleanuptool\lnk\*") do IF EXIST "%userprofile%\Desktop\%%~nxG" ECHO del "%userprofile%\Desktop\%%~nxG"


And get this message:
F:\Cleanuptool>for %G in ("F:\cleanuptool\lnk\*") do IF EXIST "C:\Users\Bruker\D
esktop\%~nxG" ECHO del "C:\Users\Bruker\Desktop\%~nxG"

F:\Cleanuptool>IF EXIST "C:\Users\Bruker\Desktop\Elkjøp Cloud.lnk" ECHO del "C:\
Users\Bruker\Desktop\Elkjøp Cloud.lnk"
del "C:\Users\Bruker\Desktop\Elkjøp Cloud.lnk"

F:\Cleanuptool>IF EXIST "C:\Users\Bruker\Desktop\Steam.lnk" ECHO del "C:\Users\B
ruker\Desktop\Steam.lnk"

F:\Cleanuptool>IF EXIST "C:\Users\Bruker\Desktop\LibreOffice 4.3.lnk" ECHO del "
C:\Users\Bruker\Desktop\LibreOffice 4.3.lnk"

F:\Cleanuptool>pause

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Delete files if exist in a diffrent one

#5 Post by ShadowThief » 25 Oct 2014 10:58

stanian wrote:F:\Cleanuptool>IF EXIST "C:\Users\Bruker\Desktop\Elkjøp Cloud.lnk" ECHO del "C:\
Users\Bruker\Desktop\Elkjøp Cloud.lnk"
del "C:\Users\Bruker\Desktop\Elkjøp Cloud.lnk"

The second line indicates that the file C:\Users\Bruker\Desktop\Elkjøp Cloud.lnk was deleted.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Delete files if exist in a diffrent one

#6 Post by Compo » 25 Oct 2014 15:10

Even though it has been mentioned, the file(s) will not actually be deleted until you are happy with the script and then remove ECHO

Post Reply