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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 6
    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


    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

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek