Tuesday 9 October 2012

Delete all files in a folder older than x

I spent half the day at work writing a VBScript to delete files in a folder last modified after a user specified number of days.


The script I wrote doesn't fit what we need, as we need to remove masses of old .log files on an Exchange server automatically, as a scheduled task, and with the file path and files last modified hard coded, rather than interactively.  But I spent a few hours on it (in between providing top quality technical support, obviously ;)  ) so I'm posting it here rather than letting the work go to waste.

On running the script, an input box prompts for a file path.  This can be local or a UNC path.  Then if the input is valid, it'll prompt for the number of days.  All files not modified within that number of days will be deleted.

The script writes to a log file that's created in the same folder as the script, detailing which files have been deleted.

Here's the code:



The script could easily be modified to not prompt for user input and just have the path and days hard coded, but, a)  that would be a waste of all my data validation work and b) they want to use powershell.  Even though I don't like powershell :(

Hope the script proves useful for someone.  Leave a comment letting me know if it helps you at all :)

******  EDIT (12 Oct 2012) ********
If you want to delete the files without actually sitting at the computer, take a look here

No comments:

Post a Comment