Something Wicked This Way Comes

Jan 9 2009   3:59PM GMT

VBScript to Delete Files in a Directory Older Than X Days

Dave Raffo Dave Raffo Profile: Dave Raffo

I recently needed to whip up a quick script to delete all files in a directory that were older than X number of days. Hopefully it is something that you can use, too. Copy the code and paste it into a text file named something like “cleanup-folder.vbs” and set the variables for strDir (the absolute path to the directory) and strDays (files older than this number of days will be deleted).

' ################################################################
' # cleanup-folder.vbs
' #
' # Removes all files older than 1 week
' # Authored by Spencer Kuziw (s.kuziw-at-epic.ca)
' # Based on code by YellowShoe
' # Version 1.0 - Sept 23 2008
' ################################################################

Dim fso, f, f1, fc, strComments, strScanDir

' user variables
' ----------------------------------------------------------------

strDir = "FULL\PATH\TO\FOLDER\TO\BE\CLEANED"
strDays = 7

' DO NOT EDIT BELOW THIS LINE
' (unless you know what you are doing)
'------------------------------------------------------------------

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(strDir)
Set fc = f.Files

For Each f1 in fc
      If DateDiff("d", f1.DateCreated, Date) > strDays Then
            'strComments = strComments & f1.name & " " & f1.DateCreated & vbCrLf
            fso.DeleteFile(f1)
      End If
Next

'wscript.echo strComments

WScript.Quit
' eof

4  Comments on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Jmazzella
    I just finished writing some configurable VBScript code for a System Admin friend of mine that is designed to be scheduled to run using the Windows Scripting Host environment. The Script will delete and/or archive files in various direct based on age of the file and file name match using regular expressions. The application is also creates a log file which is useful should someone question what was deleted or if someone wants to restore the file prior to permanent deletion. Age of the files and Name of the files are Configurable! I have the VBScript zipped up on my website and can be downloaded at [A href="http://www.code-bytes.com/Automate_WSH_VBScript_To_Delete_Or_Archive_Files.html"] Feel free to use it if it helps you, I only ask for a small donation if it helps [B]Additional detail [/B] In my situation, a number of developers had created a production application that was littering a number of production server with large data files. The volume and size of these files would threaten the server by consuming all available disk-space about once a month. The VBScript that I wrote eliminated the need to purchase additional disk-space in addition to saving the System Admin 2-3 hours per week by eliminating the need for manual cleanup of the files. Estimated cost savings from the code... about $5,000 per year just by eliminating the need of the system admin to have to manually remove these files.
    85 pointsBadges:
    report
  • iamali

    Hello Dave,

    You script is quite helpful. But there is a small issue. If I run the script manually then it does what it is suppose to. But, when I add the script in the Task Scheduler to run daily and delete files that are 7 days old then it sends up deleting all the files including the new ones.

    Is there anything I am doing wrong?

    10 pointsBadges:
    report
  • iamali
    Hello Dave,Your script is very helpful. It works fine but only when I run it manually. For some reason, when the script runs using the task scheduler, it deletes all the files including the ones that are not seven days old. Is there a particular configuration I should set in the task scheduler.
    10 pointsBadges:
    report
  • latincork
    Just found this: http://gallery.technet.microsoft.com/scriptcenter/Delete-files-folders-and-2eb27869

    I believe this guy did a good job too.

    Enjoy :)
    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: