Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I create a batch file that deletes old directories and its subdirs

Posted on 2004-08-10
9
Medium Priority
?
331 Views
Last Modified: 2012-05-05
It may work in w98 and if it is posible in nt based systems (xp, 2k)
(That should be easy, with an IF clause you should can detect the appropiate system)

I need a routine that deletes old directories

THanks
0
Comment
Question by:cpunks
  • 4
  • 2
7 Comments
 

Author Comment

by:cpunks
ID: 11764383
Or may it be the last 7 folders
0
 

Author Comment

by:cpunks
ID: 11764397
I mean, to keep the most recent up to 5 folders.
How could it be done?
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 11776524
Here is a complete solution (from my answer in http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20956707.html ).

You invoke it as follows (replace "purge" with whatever you name the script file):

               purge <folder> <maximum age in days>

For example, if the script resides in the folder itself, invoke it as follows:

               purge . 7

or if the folder is elsewhere:

               purge c:\elsewhere 7

Once you have confirmed that the echoed statements would do the correct thing, you can remove the echo statement before the del command in the line

         if !file_age! gtr %age_limit% echo del %%d

Additionally, the line

         echo File %%d is !file_age! days old.

is only there for informative purposes and may be removed.

--

mrdtn

--

@echo off

:init
     if .%2==. goto :usage
     setlocal enabledelayedexpansion
     set root_dir=%1
     set /a age_limit=%2
     call :pushdfmt

:gettoday
     set today=%date%
     set today_year=%today:~0,4%
     set today_month=%today:~4,2%
     set today_day=%today:~6,2%
     call :compdays %today_year% %today_month% %today_day% today_absolute

:gotoit
     pushd %root_dir%
     for /f %%d in ('dir /a:d /b') do (
          set folder_date=%%~td
          set /a folder_year=!folder_date:~0,4!
          set folder_month=!folder_date:~4,2!
          set folder_day=!folder_date:~6,2!
          call :compdays !folder_year! !folder_month! !folder_day! folder_absolute
          set /a folder_age=%today_absolute%-!folder_absolute!
          echo Folder %%d is !folder_age! days old.
          if !folder_age! gtr %age_limit% echo rmdir /s /q %%d
     )
     goto :end

:usage
     echo Usage: %0 [Root Path] [Age Limit in Days]
     goto :end

:end
     popd
     call :popdfmt
     goto :eof

:compdays
     :
     : Input  arguments: %1=year %2=month %3=day
     : Return arguments: %4=variable name to receive ellapsed number of days
     :
     set year=%1 & set month=%2 & set day=%3
     :
     : == Check for leading zeros
     :
     set /a year=%year%
     if .%month:~0,1%==.0 set /a month=%month:~1,1%
     if .%day:~0,1%==.0 set /a day=%day:~1,1%
     :
     : == Start by accruing all the days for past years ==
     :
     set /a ellapsed_days=(%year%-1900)*365
     :
     : == Add in days from ellapsed months ==
     :
     for %%m in (1 3 5 7 8 10)      do if %month% gtr %%m set /a ellapsed_days+=31
     for %%m in (4 6 9 11)           do if %month% gtr %%m set /a ellapsed_days+=30
     for %%m in (2)                do if %month% gtr %%m set /a ellapsed_days+=28
     :
     : == Leap year compensation ==
     :
     set /a ellapsed_days+=(%year%-1901)/4
     set /a ellapsed_days-=(%year%-1901)/100
     set /a ellapsed_days+=(%year%-1601)/400
     set current_leap=FALSE
     set /a temp=%year%%%4 && if %temp% == 0 set current_leap=TRUE
     set /a temp=%year%%%100 && if %temp% == 0 set current_leap=FALSE
     set /a temp=%year%%%400 && if %temp% == 0 set current_leap=TRUE
     if %current_leap%==TRUE if %month% gtr 2 set /a ellapsed_days+=1
     :
     : == Last but not least, add in the day of the current month ==
     :
     set /a ellapsed_days+=%day%
     :
     set /a %4=%ellapsed_days%
     :
     goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: PUSHDFMT, POPDFMT package for Windows 2k/XP
::
     goto :eof
:pushdfmt
     call :datefmt push && goto :eof
:popdfmt
     call :datefmt pop && goto :eof
:datefmt
     setlocal enabledelayedexpansion
     set tempfile=%0
     set tempfile=%tempfile::=%
     if "%1"=="push" (
          set reg_key=HKEY_CURRENT_USER\Control Panel\International
          set reg_string=sShortDate
          for %%f in (_%tempfile% __%tempfile%) do (
               if exist %%f (
                    echo Error creating temporary files.  This script will exit after a key is pressed.
                    pause > nul
                    exit
               )
          )
          reg export "!reg_key!" _%tempfile% > nul
          type _%tempfile% > __%tempfile%
          set reg_header=
          for /f "tokens=*" %%z in (__%tempfile%) do if "!reg_header!"=="" set reg_header=%%z
          for /f "tokens=3" %%v in ('reg query "!reg_key!" /v !reg_string! ^| find /i "!reg_string!"') do set date_format=%%v
          for %%f in (_%tempfile% __%tempfile%) do (
               echo !reg_header! > %%f
               echo [!reg_key!] >> %%f
          )
          >> _%tempfile% echo "!reg_string!"="yyyyMMdd"
          >> __%tempfile% echo "!reg_string!"="!date_format!"
          reg import _%tempfile% > nul
     ) else if "%1"=="pop" (
          reg import __%tempfile% > nul
          for %%f in (_%tempfile% __%tempfile%) do del %%f
          goto :eof
     ) else echo ! - DATAFMT - Bad argument.
     goto :eof
::
::PUSHDFMT, POPDFMT package for Windows 2k/XP
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 6

Expert Comment

by:mrdtn
ID: 11776567
Hmmm.  That isn't exaclty what you were looking for, was it?  The script I posted will "purge" a folder of all folders older than the number of days specified.

What you are asking for is actually simpler.  I will post in a few.

mrdtn
0
 
LVL 6

Accepted Solution

by:
mrdtn earned 420 total points
ID: 11776738
This will work on 2k and xp machines but not on 98.

Remove the echo before the rmdir after confirming that it would do the correct thing.  Adjust "keep_count" appropriately.  For Windows 98, it's not as easy.  I'll try and think of a solution for that case.  In the meantime, maybe someone else has a solution.

--

mrdtn

--

:init
      @echo off
      setlocal enabledelayedexpansion
      set /a keep_count=5
:main
      for %%n in ('dir /a:d /o:-d /b') do (
            set /a keep_count-=1
            if !keep_count! LSS 0 echo rmdir /s /q %%n
      )
      goto :end
:end
      goto :eof
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 11776833
The

"for %%n"

should actually be

"for /f %%n"

--

mrdtn
0
 
LVL 4

Expert Comment

by:MaartenG
ID: 11799300
cpunks,

here's a XP/2000 solution:

http://www.experts-exchange.com/Operating_Systems/WinXP/Q_21033074.html

Don't know about 98; that's too long ago.

Good luck,

MaartenG


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

569 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