I need a batch file that will delete files from a directory and it's subdirectories that are older than 30 days. I can't use the deltree command because there are files younger than 30 days that need to stick around. I also don't want the .bat file to have to be in the directory because I don't want it deleting itself. This is what I have so far but it only deletes files from the main directory and won't delete any from the subdirectories.
:: ---------------EXAMPLE.BAT
----------
-------
@ECHO on
:: Change directory
cd C:\Documents and Settings\user\Desktop\test
\
:: ------------
:: This is where you set the number
:: of minutes you want subtracted
:: from the current date/time.
:: ------------
SET MyDays=30
:: ------------
:: Get current date/time
:: ------------
FOR /F "TOKENS=2-4 DELIMS=/ " %%F IN ('DATE /T') DO (
SET YYYY=%%H
SET MM=%%F
SET DD=%%G
)
IF %DD% LSS 10 (SET DD=%DD:~1%)
IF %MM% LSS 10 (SET MM=%MM:~1%)
: ------------
:: Subtract days from current date.
:: ------------
SET /A DD=%DD% - %MyDays%
:: ------------
:: Do the massively painful
:: reverse calculations.. :(
:: ------------
:LoopDate
IF /I %DD% GTR 0 (GOTO DONE)
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1
:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31
goto ERROR
:SET31
set /A dd=31 + %dd%
goto LoopDate
:SET30
set /A dd=30 + %dd%
goto LoopDate
:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29
:SET28
set /A dd=28 + %dd%
goto LoopDate
:SET29
set /A dd=29 + %dd%
goto LoopDate
:DONE
IF %dd% LSS 10 set dd=0%dd%
IF %mm% LSS 10 set mm=0%mm%
for %%i in (*.*) do (
set FileName=%%i
SET FTIME=%%~ti
CALL :PROCESSFILE
)
set mm=
set yyyy=
set dd=
set thedate=
goto :EOF
:PROCESSFILE
set fyyyy=%FTIME:~6,4%
set fmm=%FTIME:~0,2%
set fdd=%FTIME:~3,2%
if /I %fyyyy% GTR 2069 set fyyyy=19%FTIME:~6,2%
:: +*************************
**********
**+
:: | This is where the files are deleted |
:: | Change the ECHO command to DEL to |
:: | delete. ECHO is used for test. |
:: +*************************
**********
**+
if /I %yyyy%%mm%%dd% GEQ %fyyyy%%fmm%%fdd% (
DEL %FileName%"
ECHO Delete %FileName%
)
ECHO Cacldate=%yyyy%%mm%%dd%
set temp=
set fyyyy=
set fmm=
set fdd=
:EXIT
:: ----------END-EXAMPLE.BAT-
----------
--
Please Help