Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Automatically delete log files older than 30 days

Posted on 2007-04-10
7
Medium Priority
?
4,605 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

715 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