Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Allowing for automated deletion in a folder


    (Page 3 of 5 )

    Most of the requests I’ve gotten were from people looking for a script that could be used as a Scheduled Task for automatically removing old backups from an archive folder.  With that purpose in mind, I decided to take this script a step further and add a subroutine that would allow the script to move through all of the files in a specified folder.

    Sub CheckFolder(objFolder, numDays)

       Set colFiles = objFolder.Files

       If colFiles.Count > 0 Then

           For Each objFile In colFiles

              Call DeleteOldFile(objFile, numDays)

           Next

       Else

           WScript.Echo "No files in folder", objFolder.Path

       End If

    End Sub

    The subroutine is pretty simple.  I use the Files property to return a collection of files in the specified folder location.  A simple check ensures that there were indeed files found and then call the DeleteOldFile method for each of them.

    To add a little flexibility, I’ve added numDays as a parameter for this subroutine.  That allows you to specify a different age for each folder.

    Let’s suppose for a minute that someone wanted to clean an entire folder tree.  That means they would want to delete files recursively, or in all subfolders, as well.  We can add recursion quite easily.

    Sub CheckFolder(objFolder, numDays, bRecurse)

       Set colFiles = objFolder.Files

       If colFiles.Count > 0 Then

           For Each objFile In colFiles

              Call DeleteOldFile(objFile, numDays)

           Next

       Else

           WScript.Echo "No files in folder", objFolder.Path

       End If

       If bRecurse Then

           Set colSubfolders = objFolder.SubFolders

           If colSubfolders.Count > 0 Then

              For Each SubFolder In colSubfolders

                  CheckFolder SubFolder, numDays, True

              Next

           End If

       End If

    End Sub

    Again keeping flexibility in mind, I’ve added bRecurse as a parameter to the subroutine.  bRecurse is a Boolean value the indicates whether or not the folder should be processed with recursion.

    If the bRecurse value is set to true, the SubFolders property is called to return a colSubfolders collection.  A quick check makes sure that the count is not equal to 0 before passing each subfolder back to the CheckFolder subroutine.

    Now we can complete the script by making a call to the CheckFolder subroutine.  Just add a line to the beginning of your script.

    numDays = 7

    strPath = "C:\Windows\Temp"

     

    Set objfso = CreateObject("Scripting.FileSystemObject")

    If objfso.FolderExists(strPath) Then

       Set objFolder = objfso.GetFolder(strPath)

       Call CheckFolder(objFolder, numDays, True)

    Else

       WScript.Echo "The specified folder", strPath, "does not exist"

    End If

    Of course, you probably know me by now.  I would never stop there.  I’ve implemented the FileSystemObject’s FolderExists method to verify the folder path before trying to connect to the Folder object.

    In the above example, I’ve set the script to recursively delete all files in the Windows Temp folder that have not been modified in the last seven days.

    Now that we’ve got a working script, let’s backtrack slightly and fix one unresolved issue.  We still need to create a subroutine that will schedule undeletable files for removal at the next reboot.

    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 3 hosted by Hostway
    Stay green...Green IT