Redpoint
asked on
batch files script
i need to write a batch file that gets the current date and deletes any log files that are 3 days older than the current date.
here is what i have do far:
echo off
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set DayOfWeek=%%i
set Month=%%j
set Day=%%k
set Year=%%l
set Date=%%i %%k/%%j/%%l
)
set Day-3
echo SiteScope%Year%_%Month%_%D ay%.log
IF EXIST C:\SiteScope\logs\SiteScop e%Year%_%M onth%_%Day %.log (
echo Deleting C:\SiteScope\logs\SiteScop e%Year%_%M onth%_%Day %.log
del C:\SiteScope\logs\SiteScop e%Year%_%M onth%_%Day %.log
echo Done
)
the problem with this is that it is limited to the current day. i need to do this for 3 days past. i was thinking of just subtracting 3 days from the "day" field but that makes problem at the turn of the month.
here is what i have do far:
echo off
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set DayOfWeek=%%i
set Month=%%j
set Day=%%k
set Year=%%l
set Date=%%i %%k/%%j/%%l
)
set Day-3
echo SiteScope%Year%_%Month%_%D
IF EXIST C:\SiteScope\logs\SiteScop
echo Deleting C:\SiteScope\logs\SiteScop
del C:\SiteScope\logs\SiteScop
echo Done
)
the problem with this is that it is limited to the current day. i need to do this for 3 days past. i was thinking of just subtracting 3 days from the "day" field but that makes problem at the turn of the month.
just copy and paste his code to a textfile and make sure you give it a vbs extension. Then modify whatever parts of the code will work for you.
here's another:
http://cwashington.netreach.net/depo/view.asp?Index=723&ScriptType=vbscript
or does it have to be a batch file?
http://cwashington.netreach.net/depo/view.asp?Index=723&ScriptType=vbscript
or does it have to be a batch file?
Hi Redpoint,
This will work in NT/2K/XP and will delete all *.LOG files in the current directory that are older than 3 days old. The age of the file is evaluated using the file's "modified" date. If you need a script which will do this based on the timestamp info in the filename, just let me know.
:: --------DELOLD.BAT-------- --
@echo off
for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
set mm=%thedate:~0,2%
set dd=%thedate:~3,2%
set yyyy=%thedate:~6,4%
set /A dd=%dd% - 3
set /A mm=%mm% + 0
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 DONE
:SET30
set /A dd=30 + %dd%
goto DONE
: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 DONE
:SET29
set /A dd=29 + %dd%
:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
for %%i in (*.log) do (
set FileName=%%i
call :PROCESSFILE %%~ti
)
set mm=
set yyyy=
set dd=
set thedate=
goto EXIT
:PROCESSFILE
set temp=%1
set fyyyy=20%temp:~6%
set fmm=%temp:~0,2%
set fdd=%temp:~3,2%
if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
:: +************************* ********** **+
:: | 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%
)
set temp=
set fyyyy=
set fmm=
set fdd=
:EXIT
:: ----------END-DELOLD.BAT-- ---------- -
pb
This will work in NT/2K/XP and will delete all *.LOG files in the current directory that are older than 3 days old. The age of the file is evaluated using the file's "modified" date. If you need a script which will do this based on the timestamp info in the filename, just let me know.
:: --------DELOLD.BAT--------
@echo off
for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
set mm=%thedate:~0,2%
set dd=%thedate:~3,2%
set yyyy=%thedate:~6,4%
set /A dd=%dd% - 3
set /A mm=%mm% + 0
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 DONE
:SET30
set /A dd=30 + %dd%
goto DONE
: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 DONE
:SET29
set /A dd=29 + %dd%
:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
for %%i in (*.log) do (
set FileName=%%i
call :PROCESSFILE %%~ti
)
set mm=
set yyyy=
set dd=
set thedate=
goto EXIT
:PROCESSFILE
set temp=%1
set fyyyy=20%temp:~6%
set fmm=%temp:~0,2%
set fdd=%temp:~3,2%
if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
:: +*************************
:: | 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%
)
set temp=
set fyyyy=
set fmm=
set fdd=
:EXIT
:: ----------END-DELOLD.BAT--
pb
ASKER
pbarrette, this is good stuff. i am actually checking the date that is in the automated file generation. for instance, the log for today would be named "filename2003_03_04.log". i only want to delete these files, not all the logs.
example:
%filename%2003_03_04.log keep
%filename%2003_03_03.log keep
%filename%2003_03_02.log keep
%filename%2003_03_01.log delete
%filename%2003_03_04.log delete
%filename%2003_03_04.log delete
the %filename" will always be the same, the concated date after the %filename% is what changes. keep all other logs.
i think that you have the award.
example:
%filename%2003_03_04.log keep
%filename%2003_03_03.log keep
%filename%2003_03_02.log keep
%filename%2003_03_01.log delete
%filename%2003_03_04.log delete
%filename%2003_03_04.log delete
the %filename" will always be the same, the concated date after the %filename% is what changes. keep all other logs.
i think that you have the award.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
pb,
with some modification i was able to take the ideas you gave me and run with them. i am crediting you b/c you pointed me in the right direction and that is what i needed. thanks ;-) !
Redpoint
with some modification i was able to take the ideas you gave me and run with them. i am crediting you b/c you pointed me in the right direction and that is what i needed. thanks ;-) !
Redpoint
http://cwashington.netreach.net/depo/view.asp?Index=729&ScriptType=vbscript