How would I write a cmd file that would delete folders that are greater than one day old?

PDSWSS
PDSWSS used Ask the Experts™
on
I  have set up  task manager in Windows Server 2008 R2 to back up an Access 2007 DB  every hour, 12 hours/day, every week day.
These files are saved as  .accdb files in  F:\backups\DATE.TIME   Everyday, 12 new  DATE.TIME folders containing the .accdb files are created in F:\backups

I need to write a .cmd file that will be triggered by task manager once a day to delete all DATE.TIME folders containing the .accdb files in F:\backups      that are  >  1 day old.
Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You could take the easy way and delete the oldest dirs over 12 of them as per examples in my scripts here:

http://scripts.dragon-it.co.uk/links/batch-versioning-backup?OpenDocument

i.e. this will loop down all directories in the directory shown in "thedir" line , newest first, and keep the number shown in "Keep" variable so as it stands it will try and run an "RD" command on each dir.  Currently it will only DISPLAY the RD command.  Remove the word echo from before the RD command if so.

Would recommend this rather than just deleting anything over a day old anyway, since if there was a problem with the backups it could mean you had no backups from today but it merrily removed yesterdays too.

@echo off
setlocal enabledelayedexpansion

REM Keep only newest files in the "old" directory:
set thedir=F:\backups
set keep=12
set count=1

cd /d %thedir%

for /F "tokens=* delims=" %%a in ('dir %thedir% /ad /o-d /b') do (
 if !count! GTR %keep% echo RD /S /Q %%a
 set /A count=count+1
)

echo All Done.

Author

Commented:
dragon-it:

Copied your code into a cmd file (retyped  "   and  '  ) and ran it. Flashes on the screen and nothing happens. Any suggestions?

BTW-  I also backup in 2 other locations every night. So removing everything > 1 day old still leaves me with other backups.

Thanks

Commented:
I would put a

pause

at the end so when your script runs, it will stop at the end where presumeably is an error, it iwll stop and ask you to press any key to continue and close the window.  Since you can't read at super human speeds, it looks like a flash to you.

Myself, I would use robocopy in this manner:

mkdir c:\delete
c:\robocopy.exe c:\Source c:\Delete /e /MOVE /MINAGE:14 /LOG+:c:\robocopy.log
rmdir c:\delete /s /q

where the new folder created is a "staging" area for my soon to be delted folder.
then source is where I want to delete from, the switch "e" is for all folders, even empty ones, the move command moves the items older than 14 days, and write to a log on the root of c for all files processed.

the last step is to delete the "staging folder"

Break this up a little before you put in the last step of remove directory, just so that it can be applied in the manner you want it to.
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Author

Commented:
tsaico:

I am an MS DOS novice. How would I create a pause at the end of the script?

Commented:
oh sorry,

hit return to go to new line, then type "pause" no quotes.

So in my example, open Notepad, past this in

mkdir c:\delete
c:\robocopy.exe c:\Source c:\Delete /e /MOVE /MINAGE:14 /LOG+:c:\robocopy.log
rmdir c:\delete /s /q
pause

and save it as a .bat file instead.  Double click that new bat file and it should give you some error most likely to do with there is no directory called source on your root of c, and then it will just wait for you to push enter.  After you have done your tests, then remove the pause before you schedule it so it can run on its own.  Then you can just check the autoschedule to see if the job ran, and if so, did it run into problems.

Author

Commented:
tsaico:
Thanks. For code provided by dragon-t -  I see a list of the files that should be deleted and then "press any key to continue"  I press any key and screen disappears and nothing is deleted.

I will try your code next.

Author

Commented:
The Backups are on the external F: not C:

How would I change your code so it will run on the F drive?  Thanks

Author

Commented:
dragon-it:
Your code is outputting the names of the files that should be deleted but is not deleting them. Any ideas?

Thanks

Author

Commented:
dragon-it:  FORGOT TO MENTION THAT WHEN I DELETED "ECHO" BEFORE "RD" I GET A MESSAGE THAT CAN NOT FIND FILE SPECIFIED. SO WITH "ECHO" ACTS AS EXPECTED. HOWEVER, WITHOUT ECHO = UNEXPECTED OUTPUT.
Thanks
Commented:
It may just be there are spaces in the filenames since I forgot some " ", try changing that line to:
echo " Working on %cd%\%%a"
if !count! GTR %keep% RD /S /Q "%%a"

and add a

PAUSE on the end like suggested before.

Steve

Author

Commented:
Excellent! That fixed the issue. Now it works.

Author

Commented:
Thanks again.

Commented:
No problem, had assumed dir names without spaces from your original example and quickly answering while passing PC inbetween nappies etc!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial