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.
Next: Deleting files based on age >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer