Batch file to delete files older than X days
Quite often, we have an requirement to delete old log files and used powershell scripting in my previous project. With this customer, they are still with Windows 2003 and have to use batch file to achieve the same.
FORFILES /p "E:\logs" /s /m *.* /d -3 /c "cmd /c if not @isdir==TRUE del @file"
- Change ‘E:\Logs’ to the directory which you are saving files
- /d -3 indicates than files older than 3 days and change it, if required
- if not @isdir == TRUE – If you have sub directories, it will skip sub directories and only delete files. Otherwise, script will delete sub directories even if you have only one day old file in the sub directory as sub directory last modified data will be greater than 3 days