?
Solved

batch files script

Posted on 2003-03-03
7
Medium Priority
?
395 Views
Last Modified: 2010-04-13
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
Comment
Question by:Redpoint
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:guidway
ID: 8060221
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
 
LVL 12

Expert Comment

by:guidway
ID: 8060236
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
 
LVL 12

Expert Comment

by:guidway
ID: 8060248
here's another:

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

or does it have to be a batch file?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 10

Expert Comment

by:pbarrette
ID: 8060290
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
 

Author Comment

by:Redpoint
ID: 8064101
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
 
LVL 10

Accepted Solution

by:
pbarrette earned 1500 total points
ID: 8065518
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
 

Author Comment

by:Redpoint
ID: 8092414
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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question