Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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 - Delete files at reboot


    (Page 4 of 5 )

    We’ve already established that if a file is currently in use it must be deleted upon reboot.  To schedule a file for deletion it needs to be added to the registry's  PendingFileRenameOperations value located under the Session Manager key for the current control set.  The exact path is in the script example below and will not change.

    The PendingFileRenameOperations value is a multi-string value that contains line pairs representing files that need to be renamed, copied, or deleted.  Each file appears on its own line.  The first of each line pair is the source and the second is the destination.

    The only way to work with multi-string value types in WSH is by using WMI’s Standard Registry Provider.  We can connect to it like this:

    Sub RemoveOnReboot(strFile)

       Const HKLM = &H80000002

       strComputer = "."

       Set objReg = GetObject("winmgmts:" & strComputer & _

           "\root\default:StdRegProv")

       strKey = "SYSTEM\CurrentControlSet\Control\Session Manager"

       strName = "PendingFileRenameOperations"

       objReg.GetMultiStringValue HKLM, strKey, strName, arrValues

    We begin our subroutine by establishing a few constants.  The first is a hex value that represents the HKEY_LOCAL_MACHINE hive in the Windows registry.  The second is used to tell WMI that we want to access the local computer.

    After using the GetObject method to connect to the registry provider, we can use the GetMultiStringValue method to read the value’s current contents and return the value data as an array named arrValues.

    Neither WSH nor WMI provide a method of checking whether or not a registry value exists.  Here I’ve provided a small workaround.  It turns out that the registry provider’s Get methods return NULL if a value does not exist and EMPTY if a value exists without any data.  However, since the GetMultiStringValue returns an array.  An array of empty strings is still considered to be null so it will return NULL in either case.

    Now a simple check will indicate whether or not there is currently any data.

       If IsNull(arrValues) Then

           arrValues = Array("\??\" & strFile, "")

    If arrValues is null we create an array with our file’s path.  Then, we add an empty string for the second element.  You’ll also notice that I’ve prefixed “\??\” to the file path.  This is required.

    If there is already data in that value, we should read the data first and then append to it.

       Else

           ReDim Preserve arrValues(UBound(arrValues) + 2)

           arrValues(UBound(arrValues) - 1) = "\??\" & strFile

           arrValues(UBound(arrValues)) = ""

    Here I’ve used a ReDim statement to add two elements to the returned array.  The Preserve flag tells VBS to retain the current information.  In the next two lines I assign my data to the two new elements.

    Look at how I’ve used the UBound function to determine my array boundaries and the position of the last two elements.  I’ve done this because the initial size of the array could be different every time the subroutine is called.

    We finish up by deleting the existing key if it is present and then write our newly formatted array back to the registry.

           objReg.DeleteValue HKLM, strKey, strName

       End If

       objReg.SetMultiStringValue HKLM, strKey, strName, arrValues

    End Sub

    This is a long one.  Let’s take a look at our script in its entirety.

    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

    - A Portable Scripting Toolbox
    - WPF Through an Example: Introduction
    - Beginning SharePoint Web Part Development
    - More Alternative Languages for WSH
    - WPF Control Layout
    - WSH in Other Languages
    - Screen Capturing via GDI+ and GDI
    - Understanding Procedures in VBScript
    - Printing Documents in WSH
    - 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





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