I have scripted Purge.bat to delete files in a specified folder whose last written date falls within a given date range.
The syntax for using Purge.bat is:
purge Folder From_YYYYMMDD To_YYYYMMDD \[/S\]
Where:
Folder is the path to the folder you want to purge. From_YYYYMMDD is the lower date in the range, using YYYYMMDD format. To_YYYYMMDD is the upper date in the range, using YYYYMMDD format. /S is an optional parameter that also purges matching files in all sub-folders.Purge.bat contains:
@echo off setlocal if \{%3\}\{\} goto Syntax set folder=%1# set folder=%folder:"=% set folder=%folder:\#=% set folder=%folder:#=% if not exist "%folder%\*.*" goto Syntax set fromdt=%2 set todt=%3 if "%fromdt%" GTR "%todt%" goto Syntax if "%fromdt:~7,1%" EQU "" goto Syntax if "%todt:~7,1%" EQU "" goto Syntax if "%fromdt:~8,1%" NEQ "" goto Syntax if "%todt:~8,1%" NEQ "" goto Syntax set xx= if \{%4\}
\{\} goto begin set xx= %4 set xx=%xx:"=% if /i "%xx%" NEQ " /s" goto Syntax :begin pushd "%folder%" for /f "Tokens=*" %%s in ('dir "%folder%\*.*" /as /ah /a-d /b%xx%') do ( set file=%%s call :parse ) popd endlocal exit /b 0 :Syntax @echo Syntax: purge Folder From_YYYYMMDD To_YYYYMMDD \[/S\] endlocal exit /b 1 :parse set file=%file:)=^)% set file=%file:(=^(% for /f "Tokens=1-3 Delims=/ " %%a in ('dir "%file%" /as /ah /a-d /TW ^|Findstr /C:"/"') do ( set /a month=100%%a%%100 set /a day=100%%b%%100 set /a year=10000%%c%%10000 ) If %year% GTR 99 goto compdt if %year% GTR 80 set year=19%year%&goto compdt if %year% LSS 10 set year=0%year% set year=20%year% :compdt if %month% LSS 10 set month=0%month% if %day% LSS 10 set day=0%day% set filedt=%year%%month%%day% if "%filedt%" LSS "%fromdt%" goto :EOF if "%filedt%" GTR "%todt%" goto :EOF del /q "%file%"
0 comments
Hide comments