Scheduling log files deletion on server with forfiles

Sharing is caring!

The following batch file uses “Forfiles.exe” for deleting files from the mentioned path. Forfiles.exe is supported only by Windows Vista, Windows 7, Windows Server 2003, and higher versions. Forfiles is present in the following path: c:>windows>system32. Before running the batch file, make sure that forfiles.exe is present in the path otherwise the batch file will not work

There are many other features for Forfiles but I have mentioned only few here.

Following command will delete IIS logs from exchange box , will run every Sunday, and will delete files in that folder older than 30 days.  

Windows 2003
at 12:00 /EVERY:Su Forfiles.exe -p C:WINDOWSsystem32LogFilesW3SVC1 -m *.log -d -30 -c “Cmd.exe /C del @path”

Windows 2008
at 12:00 /EVERY:Su Forfiles.exe -p C:inetpublogsLogFilesW3SVC1 -m *.log -d -30 -c “Cmd.exe /C del @path”

To list all of the batch files on drive C, type:

forfiles /p c: /s /m *.bat /c "cmd /c echo @file is a batch file"

 

Syntax
FORFILES [/p Path] [/m SrchMask] [/s] [/c Command] [/d [+ | -] {date | dd}]
Key
/p Path The Path to search (default=current folder)
/m SrchMask Select files matching the specified search mask
default = *.*
/s Recurse into sub-folders
/C command The command to execute for each file.
Wrap the command string in double quotes.
Default = "cmd /c echo @file"
The Command variables listed below can also be used in the
command string.
/D date Select files with a last modified date greater than or
equal to (+), or less than or equal to (-),
the specified date, using the region specific date format
typically "MM/DD/yyyy" or "DD/MM/yyyy"
/D + dd Select files with a last modified date greater than or
equal to the current date plus "dd" days. (in the future)
/D - dd Select files with a last modified date less than or
equal to the current date minus "dd" days. (in the past)

A valid "dd" number of days can be any number in
the range of 0 to 32768. (89 years)
"+" is taken as default sign if not specified.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.