Solved

Batch file to delete files older than x days

Posted on 2006-06-15
13
23,972 Views
Last Modified: 2012-05-14
I am trying to find a batch file or script that will automatically delete all log files in a specific folder older than 30 days.  I saw Pbarrette's response to question titled Batch file to Delete files older than x hours, but I can't get it to work properly.  Does anyone have such a script that you can send along?  You assistance is greatly appreciated.
0
Comment
Question by:CBS-MIS
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 14

Expert Comment

by:cwwkie
ID: 16913614
you can use xdir from PaulCaswell;

http://uk.geocities.com/xshareware/index_files/page0004.htm

the command would be:
  xdir /Execute "/Form=?(#{/ZDay=}'L'{/ZToday=}-30)DEL `*D*P*F*E`"
If you remove the /Execute you would see what would happen
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16913623
and if you really need a batchfile, take a look at this: http:Q_21657790.html
0
 

Author Comment

by:CBS-MIS
ID: 16914447
I tried this XDir without the /Execute and it would have deleted everything in the folder.  I tried to move the days out 180 and it still would have deleted everything.  Does this app look at the modified date of the files?  
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16914578
When I execute this:
    xdir "/Form=?(#{/ZDay=}'L'{/ZToday=}-170)*d `*D*P*F*E`"
I only see files from last year or before, the *d shows the last modification date before the filename.

But maybe you have found a bug in xdir for your operation system or file system.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 16914676
Here's one of my latest batch files that should do the trick. I've disabled the actual deleting of the file to testing. Also, you can pass the directory on the command line. If no parameter is specified then the current directory is used:

@echo off

setlocal

set srcDir=.
set dirMask=*.*

if not "%1"=="" set srcDir=%1

if not exist "%srcDir%" echo Directory %srcDir% does not exist.&goto :EOF

call :GETPARTS "%date%

call :SUBTRACTDAYS 30

set cutoffDate=%yy%/%mm%/%dd%

pushd.

cd /D %srcDir%

for /f "delims=" %%a in ('dir /b /a-d %dirMask%') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETPARTS %2

REM ** Remove echo from the statement below to enable the actual process
if /i "%cutoffDate%" GTR "%yy%/%mm%/%dd%" echo del %1

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

:GETPARTS

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

Good Luck,
Steve
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:GoatCreek
ID: 21349843
Hi,
I'm trying this batch job but I recieve the following error:
"70 niet verwacht op dit moment." (In english it is, "70 not expected at this moment").
I'm not a complete newbe with batch scripting, but I can't find the sollution for this. Any hints?
Thanks,
Ernst
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 21349938
Why don't you ask your own question?
0
 
LVL 2

Expert Comment

by:GoatCreek
ID: 21350361
Hey Steve, I couln'd find a way to contact you.
You want to earn some credit, right? I'll ask my question tomorrow!
0
 
LVL 2

Expert Comment

by:GoatCreek
ID: 21359005
0
 
LVL 2

Expert Comment

by:GoatCreek
ID: 21359104
0
 

Expert Comment

by:attractsupport
ID: 30538137
Hi

i am not that bright when it comes to bat file scripting, Please advise where to insert the required directory to delete delete the necessary file
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This video discusses moving either the default database or any database to a new volume.
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…

746 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

9 Experts available now in Live!

Get 1:1 Help Now