Remove All Files That Are Older Than

The following bat file will delete all files in a directory and its subdirectories that are have a modified date older than 2 years:

 
@ECHO OFF 
REM *** DELETE FILES OLDER THAN 2 YEARS  

REM *** GET TODAYS DATE 
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
  	Set Month=%%A
 	Set Day=%%B
 	Set Year=%%C
)  

REM @echo DAY = %Day% 
REM @echo Month = %Month% 
REM @echo Year = %Year%  

REM *** SUBTRACT 2 FROM THE CURRENT YEAR 
set /a Year2=%Year%-2  

forfiles /p "E:\IIS6LOGFILES" /s /m *.log /d -%Month%/%Day%/%Year2% /c "cmd /c del @path"

Leave a Reply