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

x
?
Solved

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

Posted on 2004-08-10
9
Medium Priority
?
329 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
[X]
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
  • 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

719 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