[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Batch file to delete files older than x days

Posted on 2006-06-15
13
Medium Priority
?
24,033 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

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

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

656 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