Batch to delete files in a directory older than the specified date Batch to delete files older than specified date ----------------------------------------------------- Code: @echo off setlocal :---------------------------------------: : : : Delete Files In The Current Directory : : Older Than The Specified Date : : : :---------------------------------------: :: Set delete date set _FileDate=%1 :: Check delete date arg if it does not exist goto :Usage if not defined _FileDate goto Usage :: Parse and set user date REM This is the date used to compare for /f "tokens=1,2,3 delims=/" %%a in ("%_FileDate%") do ( set "_Month=%%a" set "_Day=%%b" set "_Year=%%c" ) :: Parse month and day of files and delete files older than date for %%d in (*) do ( for /f "tokens=1,2,3,6* delims=/ " %%e in ("%%~tnxd") do ( if %%e leq %_Month% ( if %%f leq %_Day% ( if %%g leq %_Year% ( del /f /q %%h ) ) ) ) ) goto :eof :Usage echo.Usage: %0 [Date of Files] echo. echo.Example: %0 %date:~4,2%/%date:~7,2%/%date:~10,4% echo. echo.Deletes: Files Created On Or Before %date:~4,2%/%date:~7,2%/%date:~10,4%
Indeed a good recommendation, anyway thanks for the code presented seems to be helpful in my site development for T1 Internet Service site project. Definitely a good resource.
If you liked this article do nominate this article for Article of the month - Sep 2010 Before 15th of October 2010.