Solved

Batch file to delete files older than x days

Posted on 2006-06-15
13
23,979 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rename files on Windows 10 47
bat script to open stickynote with text 3 102
Help adding usb port to computer running on ms dos 20 80
How to remove unwanted words? 34 40
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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