Solved

Batch file:  Delete folders exactly 4 days old

Posted on 2007-04-05
7
17,455 Views
Last Modified: 2010-05-19
I've got a batch script and it needs to delete archive folders that are older than 4 days.  Here are the steps the batch files does (extreme pseudocode):

Delete archive folders older than 4 days
Create archive folder for today ("04_05_2007")
Run DB macro to extract files
Send files to new location
Copy files to newly created archive folder ("04_05_2007")
Exit

I'm fairly new to batch scripting and am re-using someone else's code to try to learn how to do this.  Here is how he is getting the date for today and creating the directory:

SET CurrDay=%DATE:~7,2%&::
SET CurrMonth=%DATE:~4,2%&::
SET CurrYear=%DATE:~10,4%&::
SET CurrDate=%CurrMonth%_%CurrDay%_%CurrYear%
SET archivedir="C:\Corp\Mrktg\Production\CARAT Extract\Archive"

CD /D %archivedir%
MKDIR %CurrDate%

Is there a way to build off of this existing code?  Some way to say %CurrDate%-4 or %CurrDay-4% (which does not work by the way) and then delete that specific directory that is 4 days old?  This is a process that will run everyday so during each run there should only be one directory that is being deleted, the one exactly 4 days old.

I appreciate any help in this area.  I've looked around and seen some batch solutions for things like CALL: SUBTRACTDAYS but don't see how that can be tailored to fit the current way I'm getting the date.
0
Comment
Question by:BJTurner
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 100 total points
ID: 18860876
Here's some code that will help you:

@echo off

setlocal

call :GETDATEPARTS "%date%"

set newArchive=%mm%_%dd%_%yy%

call :SUBTRACTDAYS 4

set oldArchive=%mm%_%dd%_%yy%

echo newArchive: %newArchive%
echo oldArchive: %oldArchive%

goto :EOF

:GETDATEPARTS

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

: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

Good Luck,
Steve
0
 
LVL 6

Assisted Solution

by:DocGyver
DocGyver earned 25 total points
ID: 18860942
There is not native batch command for doing the kind of date math you are asking about.  Month and year boundaries can be a real bear for that kind of math.  There are a couple of ways I can suggest to approach this.

#1 Use a for loop listing the folders in date order and delete the first one you get to.
Problem with that is if it gets run more than once in a day then you might blow away more than you plan.  Or by similar nature if the archive isn't created on some day then you may grab the wrong directory.

This will list them in forward order
for /f "usebackq" %a in (`dir /b /ad /od`)   DO (
echo %a
)


#2  Use a for loop to list the folders in reverse date order and skip the first 4 (or 3) and remove the rest

This will list them in reverseorder
for /f "usebackq" %a in (`dir /b /ad /o-d`)   DO (
echo %a
)


Like any "dir" command you can add wildcards to match patterns, thus:

dir /b /ad /o-d archive*.*
0
 
LVL 6

Expert Comment

by:DocGyver
ID: 18861044
Steve's code is pretty snazzy.  Since you can't compare the generated date to the files themselves you will only be able to match files based on each date calculation you make.  i.e.  If you subtract 4 then you can delete the archive that is 4 days old but you won't catch the one that is 5 or more without calculating that one too.  As long as the job always runs or you manually clean up if it blows up then you will be fine.

Another approach might be to use the for loop that I gave and do some comparing of the date of the file to the date subtracted date.  In pseudo code...

CalcSubtractedDate
for %a in (`dir archivefolders`) DO (
if %~ta less than CalculatedDate delete %a
)

The %~ta returns the date/time of the file returned in %a.  A little handwaving is going on with that if statement but I'm rushing out for the day.  Let me know if that sounds like an approach you'd like to take.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 30

Expert Comment

by:SteveGTR
ID: 18861082
There is no requirement to compare the date against the files. Just take the current date and subtract 4 days from it and remove the directory based on the date - 4. Check the original post.
0
 

Author Comment

by:BJTurner
ID: 18861229
Thanks guys.  Unfortunately I can't get to working the solution just yet, I've got my hands tied down with other work for tonight.  I'll get to this as soon as I can and award some points after I get it working.  Thanks again!
0
 

Author Comment

by:BJTurner
ID: 18876322
Steve's code works really well.  It's taking some time to try to understand it but the old 'plug-and-chug' worked by just pasting into my batch file.  Doc, I gave you some points too for your solution since you at least gave me another approach that I've played with as well, but most of the points went to Steve.  Thanks again guys!
0
 

Expert Comment

by:northlich
ID: 22712942
archive bit
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

It is only natural that we all want our PCs to be in good working order, improved system performance, so that is exactly how programs are advertised to entice. They say things like:            •      PC crashes? Get registry cleaner to repair it!    …
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

20 Experts available now in Live!

Get 1:1 Help Now