So in the work of doing backups, I need a batch script that would allow me to delete files in a specified directory, that are older than lets say, 3 days. This script will be set as a scheduled task to run at a specified time every day.
So in the work of doing backups, I need a batch script that would allow me to delete files in a specified directory, that are older than lets say, 3 days. This script will be set as a scheduled task to run at a specified time every day.
Where
-5
is the age of the files you want to delete (5 days or older in this case). This script is deleting.rar
files - drop the-m *.rar
if you want to delete any file type.If powershell is acceptable (should be, as its enabled by default on Server 2008+) try this:
Souce here.
If you insist on using batch files, Robocopy.exe is your answer. Its fast (multithreaded) and very robust. For your scenario you can use the following as a guide :
There is a long list of options, please do robocopy /? to see them all. You can even use it to do incremental backups, scheduling, creating backup profiles, etc.
I like to use DelEn.exe for this.
Delen - DELete ENhanced - is a souped-up version of DEL. It supports extended wildcards and parent directories, as well as date, time and size filters. Files can be excluded from deletion.
You might look at Horst Schaeffer's DelAge32:
http://home.mnet-online.de/horst.muc/wbat32.htm#top
Your command can be as simple as:
I have this command running as a scheduled task.
This is a powershell script I wrote to do what you want - it does a bit more too. I use it to clear down logs and other temporary files.
purge-dem-logs.cmd
purgelogs.ps1:
This will not work for remote computers. Admins need to manage multiple computers. Below is the script that can be used to delete folders in multiple remote computers without having to login to them.
Below script will delete folders older than 15 days. you can change the $days parameter though.
D$\Program Files (x86)\Research In Motion\BlackBerry Enterprise Server\Logs is the UNC path for Blackberry Log folder. You can change the directory where your logs/folders are located.
List all your server names in servers.txt file and it should located in the same directory as this script.
Save the script as .ps1 and run it. You can schedule it via batch file. That way you need to add Change Directory command at the beginning of the script.
Have fun.
As an alternative approach: instead of relying on querying the filesystem to get file creation times (and hitting the same files over multiple days, until they expire) you could add the file to an index of your own at time of creation. The index could potentially be as simple as a file named after the creation date, stored in a known location, with a file per line.
If you have a multithreaded/multiprocess app creating files, then you might want your index handled in a more sophisticated way.
The advantage would be that you always have a relatively simply-processed list of files created on a certain day that you can iterate over, rather than having to ask the filesystem again and again for details.
(This would rely on the app, and file creation, being managed by you, and not by a third party).