Friday 12 October 2012

Delete all files in a folder older than x (Part 2)

I wrote a script to delete all files last modified over a certain number of days a few days ago, as mentioned here.  The purpose of the script, I later found out, was to clear old Exchange log files from the email servers, rather than interactively.  The script in the linked page asks for a folder path and number of days, so whilst may be useful for some things, is no good for the purpose we needed.



We used a powershell script set up as a scheduled task to run on each of the servers each night and delete the old logs, which were filling up the servers' drive space and making them grind to a halt.  I haven't done much with powershell, but I'll be begrudgingly learning it over the next few months.  The downside to using powershell in this instance is that it needs to be installed on two of the six servers, both at locations around 140 miles from me.  That means that I need to install it remotely then schedule a reboot using a simple shutdown -r batch script, also set as a scheduled task.  One of the servers requires .Net 2 SP1 installed before I can install PS, which involved another reboot.  A pain, as using a vbscript could have had job finished this afternoon, the but it makes sense to get these things installed, even if it does mean rebooting heavily used mail servers over a weekend when I won't be there if it doesn't start up again.

Just in case anyone does need a script to delete files in this way, here's a modified version of the script that doesn't need anyone sitting there entering the file path and days at 1AM every night.  I haven't done ANY testing on this at all - I don't have a Windows machine handy at home, so if you use this, please, test thoroughly first!

I would post the PS script we used (as that allows for choosing the file extension type and will also attack sub-folders), but it's not my script to share.  I might have a go at writing my own when I get up and running this new-fangled scripting method soon.

Here's the code:



Enjoy!


No comments:

Post a Comment