Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Batch file:  Delete folders exactly 4 days old

Posted on 2007-04-05
Medium Priority
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")

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.
Question by:BJTurner
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
  • 2
  • 2
  • 2
  • +1
LVL 30

Accepted Solution

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

@echo off


call :GETDATEPARTS "%date%"

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


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

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

goto :EOF


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


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


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


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


set /A dd=31 + %dd%



set /A dd=30 + %dd%



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


set /A dd=28 + %dd%



set /A dd=29 + %dd%



if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

Good Luck,

Assisted Solution

DocGyver earned 100 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*.*

Expert Comment

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

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.
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

LVL 30

Expert Comment

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.

Author Comment

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!

Author Comment

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!

Expert Comment

ID: 22712942
archive bit

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

730 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