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

x
?
Solved

Windows 2003 Server Batch File to Remove Old Directories Based on Created Date

Posted on 2007-08-07
29
Medium Priority
?
291 Views
Last Modified: 2010-04-16
I am on Windows 2003 server.  I need a batch script that will remove old backup directories.  The directories are in the format:
08122007
08192007
08262007

I want to be able to correctly identify directories that are older than 14 days and remove them.

So far, not being a batch guru, I've been able to list the directory names in a file:

SET BACKUP_BASE=F:\oracle\DB_Backups\NEDARCD
SET COLDBACKUP_BASE=%BACKUP_BASE%\Cold_Backup
F:
CD %COLDBACKUP_BASE%

dir /B > D:\oracle\admin\Backup\check_file.txt
more D:\oracle\admin\Backup\check_file.txt
del D:\oracle\admin\Backup\check_file.txt

The batch job either needs to check either the created date or actually act on the date contained in the directory name to determine whether or not to remove the file.  I'd like to be able to change the number of retention days as our storage changes we adjust how long we can keep things
0
Comment
Question by:DonFreeman
  • 16
  • 13
29 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 2000 total points
ID: 19646316
You could try this. The processing echo's out the rd commands. To enable it remove the echo command from the line. I've marked this line with a REM statement:

@echo off

set baseDir=F:\oracle\DB_Backups\NEDARCD\Cold_Backup
set retention=14

call :GETDATEPARTS "%date%"
call :SUBTRACTDAYS %retention%

set cutoff=%yy%%mm%%dd%

pushd "%baseDir%"

REM ** Add a mask so that other directories will not be considered. This will break in 2100.
for /f "tokens=*" %%a in ('dir /b /ad "????20??" 2^>NUL') do call :PROCESS "%%a"

popd

goto :EOF

:PROCESS

set compDir=%~1
set compDir=%compDir:~4%%compDir:~0,4%

REM ** Remove echo command to enable processing
if /i "%compDir%" LEQ "%cutoff%" echo rd "%~1" /s /q

goto :EOF
a
:GETDATEPARTS

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

:SUBTRACTDAYS

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

:CHKDAY

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

:ADJUSTDAY

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

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

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

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

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

goto :EOF
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19647944
Very handy piece of work.  I found your previous solution to delete an archived file situation and thought you might pitch in on this.  Its been on my to-do list for 2 weeks and you gave me a great solution.

Thanks!
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19648012
My specialty! Glad it helped :)
0
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.

 
LVL 1

Author Comment

by:DonFreeman
ID: 19649086
I'm getting this error on including this as a file within my backup process:

** Delete Backup Folders Older Than 14 Days **
70 was unexpected at this time.

There is only one instance of "70" in the file but I'm not sure how it got there and what it is doing. Can you look at it?
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19649286
Here is the output to the screen :

D:\oracle\admin\Backup>set baseDir=F:\oracle\DB_Backups\NEDARCD\Cold_Backup

D:\oracle\admin\Backup>set retention=14

D:\oracle\admin\Backup>call :GETDATEPARTS "08072007"

D:\oracle\admin\Backup>set dt=08072007

D:\oracle\admin\Backup>set tok=1-3

D:\oracle\admin\Backup>if "0" GTR "9" set tok=2-4

D:\oracle\admin\Backup>set yyyy=

D:\oracle\admin\Backup>for /F "tokens=1-3 delims=.:/-, " %a in ('echo 08072007') do (for /F "skip=1
tokens=2-4 delims=/-,()." %x in ('echo.|date') do set %x=%a  & set %y=%b  & set %z=%c )

D:\oracle\admin\Backup>(for /F "skip=1 tokens=2-4 delims=/-,()." %x in ('echo.|date') do set %x=0807
2007  & set %y=  & set %z= )

D:\oracle\admin\Backup>set mm=08072007  & set dd=  & set yy=

D:\oracle\admin\Backup>if not "" == "" set yy=
70 was unexpected at this time.
D:\oracle\admin\Backup>if 1 LSS 1000 (if  LSS 70 (set yy=20) else (set yy=19))
D:\oracle\admin\Backup>
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19649355
I swear this thing worked when I tested it but there appears to be an error in this line:

if not "%yyyy%"=="" set yy=%yyyy%

There is a missing set of double-quotes but I'm not sure where to put them because I haven't figured out what you are doing yet.
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19649380
Nope, never mind.  I need to keep testing : ).
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19649526
This line:

call :GETDATEPARTS "08072007"

I coded it as:

call :GETDATEPARTS "%date%"

Does your %date% return 08072007?
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19649677
I copied that directly from the window after copying directly from EE to a command file and REM'ing out the @ECHO OFF.  Thats what it returned. II seems to be getting hung up in all your date logic before it ever gets to the delete function.  Its very wierd.  I tried it as soon as you posted it and it worked fine echoing the rd command with the directory it was going to remove.  I went back and tried it again and now it doesn't work.  
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19649700
Does your %date% return 08072007?

You can do this at the command prompt:

echo Date=%date%
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19653560
C:\Documents and Settings\oemrep.CERT>echo Date=%date%
Date=Wed 08/08/2007
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19653598
ok, this is wierd.   I realized that you wanted to know what %date% would be AFTER I ran the script.  I ran it, and it worked this time.  The result is:

D:\oracle\admin\Backup>Del_Backups_test.cmd
rd "07232007" /s /q

D:\oracle\admin\Backup>

0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19653763
Did you hardcode the date in the script?

call :GETDATEPARTS "08072007"
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19653789
No, I ran it with echo on and the variable was replaced by the value.  I didn't alter that at all.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19653820
That's strange. It looks like somewhere prior to the processing the date environment variable is being reset to date. I'd check any of your other processing. Very strange and very dangerous.
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19653826
I just ran it on a second server that didn't work yesterday and got:

D:\oracle\admin\Backup>Del_Backups_test.cmd
rd "07162007" /s /q
rd "07172007" /s /q
rd "08072007_SAVE" /s /q

I'll start from this point and see if I can get it running. At least I now have proof that it did work!!  I've had problems from time to time with copying and pasting things off a browser window and into a script.  Sometimes there are hidden characters or some sort of conversion error and the only way to solve it is make a copy and retype it.  I'm not sure what is going on here.  My expectation is that it is going to break when I continue.  I need to pass the target directory in a variable and unecho the rd command.  We'll see how that goes.  Thanks for you help!  Sorry to drag you back!

0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19653859
*That's strange. It looks like somewhere prior to the processing the date environment variable is being reset to date. I'd check any of your other processing. Very strange and very dangerous*

Hmm...I think I follow what you are saying.  If %date% is a system variable then maybe its not a good idea to re-use it.   Maybe it needs to be copied  into a new variable that the system doesn't use or interfere with.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19653874
You shouldn't use it as the target of a set statement. It should be used as read-only.

I did notice that I had a little garbage in my processing that can be removed:

goto :EOF
a
:GETDATEPARTS

Change to:

goto :EOF

:GETDATEPARTS
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19653896
Within the whole process I use this to create unique filenames in nearly every script:

FOR /F "tokens=1-4 delims=/ " %%i IN ('date /t') do (
   SET DATE=%%j%%k%%l
      )
SET SHUTDOWN_LOG= %ORACLE_DB_BACKUP%\LOG\Shutdown_ArcIMS_%DATE%.log
My expectation would be that control would pass to the delete script when it runs which sets the %date%  again and I would think it would remain that way until the script exits.  Isn't that true?
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19653903
Maybe I just have to set date to null at the beginning of the script.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19653905
That is the problem...
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19653910
No you should just not be messing with date.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19653918
If you want to get the date parts you can keep your code, but just name the environment variable something else:

FOR /F "tokens=1-4 delims=/ " %%i IN ('date /t') do (
   SET curDATE=%%j%%k%%l
      )
SET SHUTDOWN_LOG= %ORACLE_DB_BACKUP%\LOG\Shutdown_ArcIMS_%curDATE%.log
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19653962
"You shouldn't use it as the target of a set statement. It should be used as read-only."

Uh oh......I'm the guilty party!   That litte code snippet is used throughout the whole enterprise.  I inherited it and I'm sure I'm not going to be able to uproot it.  I'm going to have to null it before I execute your code.  Hopefully that will work.
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19653992
Yup, you're right.  I'm going to have to change my existing code.   Man, I don't know how many places that thing is used.... hundreds probably, by a lot of people...............Thanks very much.  I'm sure thats the answer.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19653999
Setting the date to nothing appears to work. I'd recommend doing at the top of my processing (in your case). You should test out these theories of yours:

test.bat ---

@echo off

set date=

echo Date=%date%

call temp.bat

temp.bat ---

@echo off

setlocal

echo %0 date=%date%
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19654005
Here's what was happening:

test.bat ---

@echo off

set date=080808

echo Date=%date%

call temp.bat

temp.bat ---

@echo off

setlocal

echo %0 date=%date%
0
 
LVL 1

Author Comment

by:DonFreeman
ID: 19654066
I would test them but you're way ahead of me : ). I have to think of how to test them and batch processing isn't my strong suit although I'm getting better.  I wouldn't have come up with your little test scenario up there.  Anyway, thanks again for your 'warranty' work : ).
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 19654080
No problem. I'm a full service coder here. Also, I learned something to watch out for and a work around provided by you. Thanks :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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…
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…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

571 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