Solved

Automatically delete log files older than 30 days

Posted on 2007-04-10
7
4,602 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Suggested Solutions

I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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