Solved

Automatically delete log files older than 30 days

Posted on 2007-04-10
7
4,595 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:
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Numerous times I have been asked this questions that what is it that makes my machine log on so slow, there have been cases where computers took 23 minute exactly after taking password and getting to the desktop. Interesting thing was the fact th…
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

919 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

19 Experts available now in Live!

Get 1:1 Help Now