Solved

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

Posted on 2004-08-10
9
324 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remote Application Install 5 69
Batch: check service status 11 90
Copy files from A to B (Only copy non existent or newer files) 7 165
Recursively Delete Files 5 83
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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