Windows Scripting
  Home arrow Windows Scripting arrow Age-Based File Deletion in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Age-Based File Deletion in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-04-17

    Table of Contents:
  • Age-Based File Deletion in WSH
  • Adding some error-handling
  • Allowing for automated deletion in a folder
  • Delete files at reboot
  • Deleting files based on age

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Age-Based File Deletion in WSH


    (Page 1 of 5 )

    I can’t count the number of times I’ve see this question asked on the internet.   I probably receive two or three requests a day asking me for a script that can automatically delete files that are older than seven days.

    So I’ve decided to make an article of it and show you just how easy it is to delete files based on age with WSH.  I’m going to help you create a good base script that can be used by its self or expanded into a much larger script.

    Since this code is most likely going to be used inside of another script I decided to write it in a modularized fashion.  In other words, it’s going to be a series of subroutines that can easily be dropped into any other script.

    To begin, we’ll connect to the FileSystemObject and create the subroutine that will perform the actual file deletion.  A simple beginning looks like this:

    numDays = 7

     

    Set objfso = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFso.GetFile("C:\test.txt")

     

    Sub DeleteOldFile(objFile, numDays)

       dateFile = objFile.DateLastModified

       dateToday = Now()

       If dateFile <= dateToday Then

           daysOld = dateToday - dateFile

           If daysOld > numDays Then

              objFile.Delete

           End If

       Else

           WScript.Echo "Incorrect date stamp in", strFile

       End If

    End Sub

    That’s exactly what you were thinking, right?  Okay, it looks a lot harder than it really is.  Here’s what we have going on in our subroutine.

    The subroutine only requires two parameters.

    The first is file object representing the file that we would like to delete.  At this point you should know how to connect to the FileSystemObject and grab a file.  If not, the first two lines aren’t too difficult to figure out—you only need to change the file path.

    The second parameter is an integer representing the file age in days.

    Next we establish two very important dates:  the file’s Last Modified date and today’s date.  It would be pretty tough to figure out a file’s age without those two pieces of information.  The File object’s DateLastModified property and VBScript’s Now() function both return date type variables.

    Because date types are special, they can be calculated much the same way you would a number.  Adding or subtracting them results in a floating point number of days.  So that’s exactly what we do to determine daysOld.

    Finally, a simple If statement determines if the file is older than the given number of days (numDays).  If it is, we use the File object’s Delete method.  If it isn’t, we leave it alone.

    I’ve also added a little error handling in there that will flag if a file’s date is newer than today’s date.  That would indicate a problem in the file’s date stamp, or a change in the system time since the file was last modified.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · This article answers one of the most asked questions about scripting. Hope you...
     

    WINDOWS SCRIPTING ARTICLES

    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions
    - VBScript: Functioning with Strings
    - Working with the Windows Registry in C++
    - Understanding Objects
    - HTML Applications: Giving WSH a User Interfa...
    - Modifying Computer Objects with Active Direc...
    - Logon Script to Send Email Notifications
    - Securing Computers and Active Directory
    - Moving and Renaming Computers with Active Di...
    - Working with System Processes in WSH





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway