Solved

Batch file to delete files older than x days

Posted on 2006-06-15
13
23,982 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
[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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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