Solved

Automatically delete log files older than 30 days

Posted on 2007-04-10
7
4,603 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
[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
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:
Danny Child 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 23

Expert Comment

by:Danny Child
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:Danny Child
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This may not be a text book method to resolve VSS backup issues but it seemed to have worked on few of the Windows 2003 servers we had issues while performing a Volume Shadow Copy backup. If you have issues while performing a shadow copy backup usin…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 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