Delete files older than 7 days with log file and history

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
newbie2013
Posts: 16
Joined: 26 Aug 2014 22:57

Delete files older than 7 days with log file and history

#1 Post by newbie2013 » 21 Sep 2014 21:03

I am using below command in batch file to delete files older than 7 days and scheduled it daily. It is working fine.

Code: Select all


forfiles -p "D:\Target\ReportOutput" -s -m *.* /D -7 /C "cmd /c del @path"



I would like to track the files it is deleting daily. Please give me a code to have a log file and it has to generate a new log file daily.

OS: Microsoft Windows Server 2003.

Many thanks in advance for looking at this enquiry. I'll look forward to hear from you.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Delete files older than 7 days with log file and history

#2 Post by penpen » 22 Sep 2014 03:54

I'm running Windows xp (with no forfiles) so i can't test it, but i think the following may help you:

Code: Select all

forfiles -p "D:\Target\ReportOutput" -s -m *.* /D -7 /C "cmd /q /d /c del @path & >> LogDel_%date%.log echo @path"
Maybe the ampersand has to be escaped based on how forfiles executes the command at the end.

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete files older than 7 days with log file and history

#3 Post by foxidrive » 22 Sep 2014 06:55

It does work in Windows 8 penpen, but depends on the %date% format and can be simplified.

Code: Select all

forfiles -p "D:\Target\ReportOutput" -s -m *.* /D -7 /C "cmd /c del @path & echo @path">> "LogDel_%date:/=-%.log"

Using penpen's code, the log file is created in the folder shown.

The version above should work in more regions (because of %date% differences) and writes the log file in the same folder as the batch file.

Post Reply