Solved

Batch file to delete files older than x days

Posted on 2006-06-15
13
23,980 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Title # Comments Views Activity
Bad characters in csv top row. 4 84
search for string in file name and copy matching files to directory 6 67
Batch File Bulk File Rename 3 61
MailSend Attachments 41 184
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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