Page 1 of 1

Deleting old directories

Posted: 03 Jan 2008 18:35
by forum7777777
Howdy

In windows 2000 I need to go to a directory and delete any subdirectories ( and their files ) that are more than 1 week old.

I know a little about batch programming, but this really has me confused.

Anyone know anything about how to do this or have code to do it please?

thanks

Frank.

Posted: 04 Jan 2008 02:15
by DosItHelp
Frank,

Date / Time calculation in batch is tricky. Using batch functions you can minimize the clutter in your main script to the following:

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS

set "days=7"
set "srcpath=%temp%"

call:jdate tnow "%date%"
for /f "tokens=*" %%F in ('dir /b "%srcpath%.\*.*"') do (
    call:DeleteIfOld "%%~$srcpath:F" "%days%" "%tnow%"
)
ECHO.
PAUSE
GOTO:EOF


You will need the following functions:
:DeleteIfOld
:ftime
:jdate
:date2jdate
:jdate2date

Here they are:

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS

set "srcpath=%temp%"
call:jdate tnow "%date%"
for /f "tokens=*" %%F in ('dir /b "%srcpath%.\*.*"') do (
    call:DeleteIfOld "%%~$srcpath:F" "449" "%tnow%"
)
ECHO.
PAUSE
GOTO:EOF


::-----------------------------------------------------------------------------------
::-- Functions start below here
::-----------------------------------------------------------------------------------


:DeleteIfOld name days tnow -- deletes file or directory if older than given number of days
::                         -- name [in] - name of file or directory
::                         -- days [in] - number of days to expire
::                         -- tnow [in] - today's date in julia days
:$source www.DosTips.com
SETLOCAL
set "days=%~2"
set "tnow=%~3"
call:ftime tfile "%~1"
set /a "diff=tnow-tfile"
if %diff% LEQ %days% EXIT /b %ERRORLEVEL
set "attr=%~a1"
rem ECHO.%attr%, %attr:~0,1%, %~nx1 is %diff% days old
if /i "%attr:~0,1%"=="d" (
    rd /Q /S "%~1"
) ELSE (
    del /Q "%~1"
)
EXIT /b %ERRORLEVEL%


:ftime JD filename attr -- compares the time of two files, succeeds if condition is met, fails otherwise
::                  -- JD    [out]    - valref file time in julian days
::                  -- attr  [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
::source www.DosTips.com
SETLOCAL
set file=%~2
set attr=%~3
if "%attr%"=="" set attr=/tw
for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-1]""') do set T=%%a
call:jdate JD "%T%"
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b %ERRORLEVEL%


:jdate JD DateStr -- converts a date string into julian day number
::                -- JD      [out] - julian days
::                -- DateStr [in]  - date string, i.e. "03/31/2006" or "Fri 03/31/2006"
::source www.DosTips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set DateStr=%~2
set DateStr=%DateStr:~-10%&rem consider only the last 10 characters
for /f "tokens=1-3 delims=/ " %%a in ("%DateStr%") do (
    set /a YYYY=1%%c-10000
    set /a MM=1%%a-100
    set /a DD=1%%b-100
)
call:date2jdate JD "%YYYY%" "%MM%" "%DD%"
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b %ERRORLEVEL%


:date2jdate JD YYYY MM DD -- converts a gregorian calender date into julian day format
::                     -- JD   [out] - julian days
::                     -- YYYY [in]  - gregorian year, i.e. 2006
::                     -- MM   [in]  - gregorian month, i.e. 12 for december
::                     -- DD   [in]  - gregorian day, i.e. 31
::                     -- reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
::source www.DosTips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set YYYY=%~2
set MM=%~3
set DD=%~4
set /a I=%YYYY%
set /a J=%MM%
set /a K=%DD%
set /a JD=K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100)/4
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b %ERRORLEVEL%


:jdate2date JD YYYY MM DD -- converts julian days into gregorian date format
::                     -- JD   [in]  - julian days
::                     -- YYYY [out] - gregorian year, i.e. 2006
::                     -- MM   [out] - gregorian month, i.e. 12 for december
::                     -- DD   [out] - gregorian day, i.e. 31
::                     -- reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
::source www.DosTips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569
set /a N= 4*L/146097
set /a L= L-(146097*N+3)/4
set /a I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31
set /a J= 80*L/2447
set /a K= L-2447*J/80
set /a L= J/11
set /a J= J+2-12*L
set /a I= 100*(N-49)+I+L
set /a YYYY= I
set /a MM= J
set /a DD= K
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET %~2=%YYYY%
    IF "%~3" NEQ "" SET %~3=%MM%
    IF "%~4" NEQ "" SET %~4=%DD%
)
EXIT /b %ERRORLEVEL%


DosItHelp? :wink: