Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

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.

0
Redpoint
Asked:
Redpoint
  • 3
  • 2
  • 2
1 Solution
 
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
[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
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
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now