Batch file: Delete folders exactly 4 days old

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.
BJTurnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DocGyverCommented:
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
DocGyverCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

SteveGTRCommented:
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
BJTurnerAuthor Commented:
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
BJTurnerAuthor Commented:
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
northlichCommented:
archive bit
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.