Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - 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 
Mobile Linux 
App Generation ROI 
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


    Age-Based File Deletion in WSH - Adding some error-handling


    (Page 2 of 5 )

    When I create scripts I like to account for as many different scenarios as possible.  This script is no different.  It’s hard to say what kind of uses different people may have for something like this.  That also means that there’s no telling what files they may try to delete.

    In Windows you will receive an error any time you try to delete a file that is read-only.  With that in mind we should attempt to work around that.

    We could easily add an If statement to check the file’s attributes and change them whenever necessary, but that would be more work that is really required.  There’s no point wasting space checking a file’s attributes when we can simply reset them before we delete.  Let’s take a look at the code.

    Sub DeleteOldFile(objFile, numDays)

       dateFile = objFile.DateLastModified

       dateToday = Now()

       If dateFile <= dateToday Then

           daysOld = dateToday - dateFile

           If daysOld > numDays Then

              objFile.Attributes = 0

              objFile.Delete

           End If

       Else

           WScript.Echo "Incorrect date stamp in", strFile

       End If

    End Sub

    Quite simply we’re using the File object’s Attributes property to reset any attributes prior to calling the Delete method.  There’s no harm in setting this attribute in every case so there’s no need to create any If statement at all.

    There is still one last scenario to consider—an undeletable file.  Since we’ve reset the file’s attributes, the only scenario left that would prevent a file from being deleted is if the file was currently in use.

    This would raise an error and cause our script to quit.  For obvious reasons, we don’t want that to happen.  We want our script to continue on to the next file.  In order for that to happen, we need to turn off error handling with the On Error Resume Next statement.

    Sub DeleteOldFile(objFile, numDays)

       On Error Resume Next

       Err.Clear

       dateFile = objFile.DateLastModified

       dateToday = Now()

       If dateFile <= dateToday Then

           daysOld = dateToday - dateFile

           If daysOld > numDays Then

              strFile = objFile.Path

              objFile.Attributes = 0

              objFile.Delete

              If Err.number <> 0 Then

                  WScript.Echo Err.number, Err.Description, Err.Source, _

                     strFile

                  Call RemoveOnReboot(strFile)

              End If

           End If

       Else

           WScript.Echo "Incorrect date stamp in", strFile

       End If

    End Sub

    I’ve made a few final changes to complete the DeleteOldFile subroutine.  These changes are mostly for error-handling purposes.

    First, I’ve made sure to clear the Err object after entering the subroutine.  Then I’ve done a simple error check to see if the object file was able to be deleted.  A 0 return code indicates a successful deletion, anything else indicates an error.  For instance, an error code of 70 would indicate that the target file was in use.

    If there was an error, I can safely assume it’s because the file is in use.  That means that the file should be scheduled for deletion after the next reboot.  We’ll get in to that later.  For now, just pass the file path to the RemoveOnReboot subroutine that we’ll be creating later.

    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

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT