if there is more then 1 file then delete old files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DenotsNiaga
Posts: 3
Joined: 15 Dec 2011 01:43

if there is more then 1 file then delete old files

#1 Post by DenotsNiaga » 15 Dec 2011 01:55

i run a backup to a share and i need to delete old backup's
otherwise my share will run out of disk space

now i run this in file in Scheduled Tasks

@ECHO OFF
IF "%DAY%" == "Monday" GOTO FIRST

:SECOND
forfiles /p d:\bacup /s /m *.* /d -2 /c "cmd /c del @file : date >= 1 day > NUL"
GOTO END

:FIRST
forfiles /p d:\backup /s /m *.* /d -4 /c "cmd /c del @file : date >= 3 days > NUL"
GOTO END

:END

wich works fine, but i need to keep at least 1 file.
so if my backup fails for 2 days, this file will delete my last good backup.

i need to add that i will always keep the last file in this directory.

so something like, if there is only one file then DONT delete anything.

thanks

aGerman
Expert
Posts: 4722
Joined: 22 Jan 2010 18:01
Location: Germany

Re: if there is more then 1 file then delete old files

#2 Post by aGerman » 15 Dec 2011 10:36

Someting like that?

Code: Select all

dir /a-d /b /s "d:\bacup\*" >nul 2>&1 ||goto :eof

Regards
aGerman

DenotsNiaga
Posts: 3
Joined: 15 Dec 2011 01:43

Re: if there is more then 1 file then delete old files

#3 Post by DenotsNiaga » 16 Dec 2011 04:27

thanks for the response but it still deletes all older files when there is only one file in there

now i have this
dunno if i do this correct

dir /a-d /b /s "d:\bacup\*" >nul 2>&1 ||goto :eof

@ECHO OFF
IF "%DAY%" == "Monday" GOTO FIRST

:SECOND
forfiles /p d:\bacup /s /m *.* /d -2 /c "cmd /c del @file : date >= 1 day > NUL"
GOTO END

:FIRST
forfiles /p d:\backup /s /m *.* /d -4 /c "cmd /c del @file : date >= 3 days > NUL"
GOTO END

:END

Post Reply