Batch file help

J

John

Trying to write a DOS batch program that will delete old server log files.
Want to schedule this using AT or similar.

Is there a series of batch commands I can use to delete files older than 1
month, etc? Or are the batch commands too limited and I'll need a 3rd
party application?

If I need a 3rd party app, any suggestions would be appreciated.

John
 
G

Guest

if you are using date formulas as part of the log name you
can do something like the following (treak it to your
needs). I kind of shortened it a bit just to give you
some ideas. try www.labmice.net for more info on
scripting or Rob VanWonderworld (think that's the name of
the guy/site). If you search for Windows Batch file
commands you'll hit a ton of sites. You can also use the
same formula (basically) as extracting the date to parse
the date/time from the filestamp if you aren't using dates
in the log file naming convention. I'd say you definitely
don't need to pay for anything....maybe a couple hours of
time to do the research

========================

@echo off

REM ***********************
REM Created by James Shaver
REM (e-mail address removed)
REM Revision 1: Oct 6, 2003
REM ***********************

REM ***********************
REM Set Date variables
REM ***********************

for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set DayOfWeek=%%i
set Month=%%j
set Day=%%k
set Year=%%l
)

REM ***********************
REM subtract 4 days to know date of backup file to delete
REM ***********************

set Lday=%Day%
set /A Lday -= 4
set zero = 0

REM ***********************
REM If day is single integer and 4 or less go to previous
month for month variable
REM ***********************


if %Day%=01 GOTO Mday
if %Day%=02 Goto Mday
if %Day%=03 Goto Mday
if %Day%=04 Goto Mday

REM ***********************
REM if date of backup to delete day is 5 or greater stay
in same month
REM ***********************

if %Day%=05 Goto Backo
if %Day%=06 Goto Backo
if %Day%=07 Goto Backo
if %Day%=08 Goto Backo
if %Day%=09 Goto Backo

goto Backp

:Mday
if %Month%=01 Goto :Jan
if %Month%=02 Goto :Feb
if %Month%=03 Goto :Mar
if %Month%=04 Goto :Apr
if %Month%=05 Goto :May
if %Month%=06 Goto :Jun
if %Month%=07 Goto :Jul
if %Month%=08 Goto :Aug
if %Month%=09 Goto :Sep
if %Month%=10 Goto :Oct
if %Month%=11 Goto :Nov
if %Month%=12 Goto :Dec

:Jan
set /A Lday += 31
set Month =12
Goto Backp

:Feb
set /A Lday += 31
set Month = 01
Goto Backp

REM ***********************
REM Check for leap year
REM ***********************

:Mar
if %Year% = 2004 set /A Lday += 29
if %Year% = 2008 set /A Lday += 29
if %Year% = 2012 set /A Lday += 29
if %Year% = 2016 set /A Lday += 29
else set /A Lday += 28
set Month = 02
Goto Backp

:Apr
set /A Lday += 31
set Month = 03
Goto Backp

:May
set /A Lday += 30
set Month = 04
Goto Backp

:Jun
set /A Lday += 31
set Month = 05
Goto Backp

:Jul
set /A Lday += 30
set Month = 06
Goto Backp

:Aug
set /A Lday += 31
set Month = 07
Goto Backp

:Sep
set /A Lday += 31
set Month = 08
Goto Backp

:Oct
set /A Lday += 30
set Month = 09
Goto Backp

:Nov
set /A Lday += 31
set Month = 10
Goto Backp

:Dec
set /A Lday += 30
set Month = 11
Goto Backp

REM ***********************
REM Use current day variable to copy current file and use
Lday variable to delete log from 4 days ago
REM ***********************

:Backp
del e:\logfile_db-%Year%%Month%%Lday%_01_03_11

Goto End

REM ***********************
REM Use current day variable to copy current file and use
Lday variable to delete log from 4 days ago
REM Since Lday is single digit also use Zero variable to
add the place holder
REM ***********************

:Backo
del e:\logfile_db-%Year%%Month%%Zero%%Lday%_01_03_11

:End
exit
 
P

Phil Robyn

John said:
Trying to write a DOS batch program that will delete old server log files.
Want to schedule this using AT or similar.

Is there a series of batch commands I can use to delete files older than 1
month, etc? Or are the batch commands too limited and I'll need a 3rd
party application?

If I need a 3rd party app, any suggestions would be appreciated.

John

Of course, this can easily be done using a batch file, possibly in conjunction
with Windows Scripting Host. Here's an example:

=====begin C:\cmd\demo\TestDelFilesOlderThan.cmd ====================
01. @echo off
02. ::
03. :: deletes any files matching (filespec) that are
04. :: older than (cutoff), where (cutoff) is specified
05. :: as '-nnnn' (number of days)
06. ::
07. :: example: demo\TestDelFilesOlderThan c:\cmd\test\x*.cmd -100
08. ::
09. setlocal
10. set filespec=%1
11. set cutoff=%2
12. call WSHDate today %cutoff%
13. set cutoff=%WSHDate%
14. for %%a in (%filespec%) do call :main %%~fa %%~ta
15. goto :EOF
16. :main
17. set zfile=%1
18. set zdate=%2
19. for /f "tokens=1-3 delims=/ " %%a in (
20. "%zdate%"
21. ) do set fmm=%%a&set fdd=%%b&set fyy=%%c
22. if "%fyy:~0,1%" equ "0" (
23. set fyy=20%fyy%
24. ) else (
25. set fyy=19%fyy%
26. )
27. set zdate=%fyy%%fmm%%fdd%
28. if %zdate% lss %cutoff% echo %zdate% %zfile%
29. goto :EOF
=====end C:\cmd\demo\TestDelFilesOlderThan.cmd ====================
=====begin C:\cmd\UTIL\WSHDate.cmd ====================
01. @echo off
02. setlocal
03. set date1=%1
04. set qty=%2
05. if /i "%date1%" EQU "TODAY" (
06. set date1=now
07. ) else (
08. set date1="%date1%"
09. )
10. echo>%temp%\%~n0.vbs s=DateAdd("d",%qty%,%date1%)
11. echo>>%temp%\%~n0.vbs WScript.Echo year(s)^&right(100+month(s),2)^&right(100+day(s),2)
12. for /f "tokens=1" %%a in (
13. 'cscript //nologo %temp%\%~n0.vbs'
14. ) do set result=%%a
15. endlocal&set %~n0=%result%
=====end C:\cmd\UTIL\WSHDate.cmd ====================

Line 28 of C:\cmd\demo\TestDelFilesOlderThan.cmd is currently just displaying
the name and date of any file that matches the date-selection criterion. To
actually delete the matching files, you will have to modify line 28 appropriately.

You can also do this using FORFILES.EXE, which can be freely downloaded from
Microsoft at

ftp://ftp.microsoft.com/ResKit/y2kfix/x86/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top