Solved

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

Posted on 2004-08-10
9
325 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
9 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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