PDSWSS
asked on
How would I write a cmd file that would delete folders that are greater than one day old?
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
tsaico:
I am an MS DOS novice. How would I create a pause at the end of the script?
I am an MS DOS novice. How would I create a pause at the end of the script?
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.
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.
ASKER
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.
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.
ASKER
The Backups are on the external F: not C:
How would I change your code so it will run on the F drive? Thanks
How would I change your code so it will run on the F drive? Thanks
ASKER
dragon-it:
Your code is outputting the names of the files that should be deleted but is not deleting them. Any ideas?
Thanks
Your code is outputting the names of the files that should be deleted but is not deleting them. Any ideas?
Thanks
ASKER
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
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent! That fixed the issue. Now it works.
ASKER
Thanks again.
No problem, had assumed dir names without spaces from your original example and quickly answering while passing PC inbetween nappies etc!
ASKER
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