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%_%Day%.log


IF EXIST C:\SiteScope\logs\SiteScope%Year%_%Month%_%Day%.log (
        echo Deleting C:\SiteScope\logs\SiteScope%Year%_%Month%_%Day%.log
        del C:\SiteScope\logs\SiteScope%Year%_%Month%_%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.

RedpointAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guidwayCommented:
this might be something you want to try: it is a vbscript but appears to give all the options you want.


http://cwashington.netreach.net/depo/view.asp?Index=729&ScriptType=vbscript
0
guidwayCommented:
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.

0
guidwayCommented:
here's another:

http://cwashington.netreach.net/depo/view.asp?Index=723&ScriptType=vbscript

or does it have to be a batch file?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

pbarretteCommented:
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
0
RedpointAuthor Commented:
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.
0
pbarretteCommented:
Hi Redpoint,

I'm not quite sure I get it.

You have a file, let's call it "MyLogYYYY_MM_DD.LOG" and you want to delete two instances of:
MyLog2003_03_04.log

but you want to keep:
MyLog2003_03_04.log

How do you propose doing that?

If you want to delete the files based on the filename, then you could replace this code:
--------------------
: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%
--------------------

With this:
--------------------
: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 %%I
)

set mm=
set yyyy=
set dd=
set thedate=
goto EXIT

:PROCESSFILE
set temp=%1
set temp=temp:MyLog=%
set fyyyy=%temp:~0,4%
set fmm=%temp:~5,2%
set fdd=%temp:~8,2%
if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
--------------------

Be sure to change:
set temp=temp:MyLog=%

to reflect the actual filename header of your file.

Alternately, if "MyLog2003_03_04.log" was last modified on 4-MAR-2003 and "MyLog2003_03_01.log" was last modified on 1-MAR-2003, and this is always true of your logs, then you can use my first script with no modification. It will work fine.

You can also replace:
DEL %FileName%

With:
ECHO %FileName%

to test the operation of the script. It will simply display the files that the script will be parsing, instead of actually deleting them.

Hope this helps,
pb
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RedpointAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 2000

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.