Solved

Automatically delete log files older than 30 days

Posted on 2007-04-10
7
4,594 Views
Last Modified: 2010-04-08
Hi,
We backup some of our log files to a dedicated disk, but with time it runs out of space.

I therefore need a command that can delete files older than X days.

I can then schedule it to every night...

It's on a Windows 2003 Std. server

Regards

Peter
0
Comment
Question by:pctools
7 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18884481
Here's a batch file that accepts optional parameters. The first is the base directory to start processing from and the second is the number of days to check. The defaults are the current directory (.) and 30 days.

@echo off

setlocal

call :GETDATEPARTS "%date%"

set dayCnt=30

if not "%~2"=="" set dayCnt=%~2

call :SUBTRACTDAYS %dayCnt%

set cutoff=%yy%%mm%%dd%

set workDir=.

if not "%~1"=="" set workDir=%~1

echo Scanning directory (%workDir%) looking for files %dayCnt% old to delete...

pushd "%workDir%"

for /f "tokens=*" %%a in ('dir /b /s /a-d 2^>NUL') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETDATEPARTS "%~2"

if /i "%yy%%mm%%dd%" GTR "%cutoff%" goto :EOF

echo Deleting %~1 last changed %mm%/%dd%/%yy%...
del "%~1"

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 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
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

Good Luck,
Steve
0
 
LVL 30

Expert Comment

by:LauraEHunterMVP
ID: 18884503
The following article provides a script that will delete all files older than a specified date: http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov04/hey1104.mspx

Hope this helps.

Laura E. Hunter - Microsoft MVP: Windows Server - Networking
0
 
LVL 23

Accepted Solution

by:
DanCh99 earned 500 total points
ID: 18958094
try the FORFILES.EXE server resource kit tool - works on NT, 2000, XP, 2003, etc

example:
FORFILES -pc:\foldername -s -m*.* -d-30 -c"CMD /C del @FILE"

-p = path
-s = include subdirs
-m = match filetype
-d = age in days (can also be set as an absolute date ie DDMMYYYY)
-c = command to execute

Note there are no spaces between the switches and their arguments.
info here:
http://www.jsiinc.com/SUBL/tip5600/rh5645.htm
Download it here:
http://www.dynawell.com/support/ResKit/winnt.asp
http://www.dynawell.com/support/ResKit/

hth, Danny
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 23

Expert Comment

by:DanCh99
ID: 18958102
also, Robocopy can move files based on their date, and also some Backup jobs can be set to Backup, then Delete (archiving, in effect)
0
 
LVL 23

Expert Comment

by:DanCh99
ID: 19000437
have you had a chance to try FORFILES yet?  it WILL do what you want!
0
 
LVL 3

Expert Comment

by:dcohn
ID: 20425413
I thought I would mention that FORFILES included in W2003 server no longer accepts the parameters with the - but requires the /  and requires a space between the parameter and the command.

Instead of

-pc:\path  you need  /p c:\path

Drove me nuts


FORFILES [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

This does not work in W2003  FORFILES -pc:\foldername -s -m*.* -d-30 -c"CMD /C del @FILE"


0
 
LVL 3

Expert Comment

by:dcohn
ID: 20425434
OH sorry.  Forfiles is a good simple solution especially compared to that insane 100 line batch file posted up above but damn  I use 4NT as my command processor and it has a very small parameter to delete files by data.  Six characters after del and you are done.  It is a dream.  JPsoft is the maker  No connection to them.

del  /[d-30,1/1/80] c:\pathtostuff\*.log

deletes all log files over 30 days old in the dir specified.  Just write a BTM or set 4NT as your cmd processor.

Crazy easy.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now