Skip navigation

JSI Tip 0713. I want to delete and [archive] all files in a directory, and optionally in its' sub-dirctories, which are xx days old.


DELOLD will delete files older than xx days, but it won't archive them and it won't search sub-directories.

I have written JSIOLD.BAT. The useage is:

JSIOLD "<Path>" \["<Archive Path>"\] \[Age\] \[/s\] where:

 Parameter   D e s c r i p t i o n 
 "<Path>"   The directory you want to search. 
 "<Archive Path>"      The optional path to the archive. 
 Age   The number of days. If omitted, the default is 60 days.                 
 /s   Optional sub-directory switch. 

NOTE: This script is aware of leap years.

JSIOLD.BAT

@echo off 
setlocal
set /a agen=60
set subd=
set suby=
if not exist %1\*.* goto notx
if "%4"

"" goto p4n if /i "%4"

"/s" goto p4s goto syntax :p4s set suby=/s set agea=%3 set /a agen=%agea% if "%agea%"

"%agen%" goto p2y goto syntax :p4n if "%3"

"" goto p2 if /i "%3"

"/s" goto p3s set agea=%3 set /a agen=%agea% if "%agea%"

"%agen%" goto p2y :syntax @echo Syntax error: JSIOLD "" \[""\] \[Age\] \[/s\] :endc endlocal goto end :ape @echo Path %1 equals Archive %2 goto syntax :nota @echo Archive %2 not found. goto syntax :notx @echo Directory %1 not found. goto syntax :p2 if "%2"

"" goto today if /i "%2"

"/s" goto lists set agea=%2 set /a agen=%agea% if "%agea%"

"%agen%" goto today :p2y if not exist %2\*.* goto nota set subd=%2 if /i %1

%2 goto ape goto today :lists set suby=/s goto today :p3s set agea=%2 set /a agen=%agea% if "%agea%"

"%agen%" goto today if not exist %2\*.* goto syntax set suby=/s set subd=%2 if /i %1

%2 goto ape :today for /f "tokens=1,2,3,4* delims=/ " %%i in ('date /t') do set TDDAY=%%i&set TDMM=##%%j&set TDDD=##%%k&set TDYY=%%l set TDMM=%TDMM:##0=##% set TDMM=%TDMM:##=% set TDDD=%TDDD:##0=##% set TDDD=%TDDD:##=% set /a TDDD=%TDDD% set /a TDMM=%TDMM% set /a TDYY=%TDYY% set /a TDDD=%TDDD% - %agen% :aday if %TDDD% GEQ 1 goto dir set /a TDMM=%TDMM% -1 if %TDMM% GEQ 1 goto aday%TDMM% set /a TDYY=%TDYY% - 1 set /a TDMM=%TDMM% + 12 goto aday%TDMM% :aday1 set /a TDDD=%TDDD% + 31 goto aday :aday2 set /a TDDD=%TDDD% + 28 set /a WKYY=%TDYY% / 4 set /a WKYY=%WKYY% * 4 If %WKYY% NEQ %TDYY% goto aday set /a TDDD=%TDDD% + 1 goto aday :aday3 set /a TDDD=%TDDD% + 31 goto aday :aday4 set /a TDDD=%TDDD% + 30 goto aday :aday5 set /a TDDD=%TDDD% + 31 goto aday :aday6 set /a TDDD=%TDDD% + 30 goto aday :aday7 set /a TDDD=%TDDD% + 31 goto aday :aday8 set /a TDDD=%TDDD% + 31 goto aday :aday9 set /a TDDD=%TDDD% + 30 goto aday :aday10 set /a TDDD=%TDDD% + 31 goto aday :aday11 set /a TDDD=%TDDD% + 30 goto aday :aday12 set /a TDDD=%TDDD% + 31 goto aday :dir if %TDDD% GTR 9 goto dirdd set TDDD=0%TDDD% goto dirm :dirdd set TDDD=%TDDD% :dirm if %TDMM% GTR 9 goto dirmm set TDMM=0%TDMM% goto diryy :dirmm set TDMM=%TDMM% :diryy set TYMD=%TDYY%%TDMM%%TDDD% echo %TYMD% for /f "Tokens=*" %%i in ('dir %1\*.* /B') do call :jsioldc %1\"%%i" if "%suby%"

"" goto endc for /f "Tokens=*" %%i in ('dir %1\*.* /B /AD /S') do call :jsioldcd "%%i" goto endc :jsioldcd for /f "Tokens=*" %%z in ('dir %1\*.* /B') do call :jsioldc %1\"%%z" goto end :jsioldc set /a cnt=0 for /f "Tokens=1-4* Delims=/ " %%a in ('dir %1 /N') do call :jsioldd %1 %%a %%b %%c %%d %%e goto end :jsioldd set /a cnt=%cnt% + 1 if not %cnt% EQU 4 goto end if "%6"

"" goto end set FMM=%2 set FDD=%3 set FYY=%4 set FYMD=%FYY%%FMM%%FDD% if %FYMD% GEQ %TYMD% goto end echo %FYMD% %1 %subd% DEL if %subd%=="" goto jsiolddd copy %1 %subd%\*.* :jsiolddd del /q /f %1 :end



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish