Implementing the CompressedFolder Class - Examining the Support Methods
(Page 3 of 4 )
The support methods I’ve created simply add functionality to my class. They are the various functions and routines that are used in conjunction with the ones you saw on the last page. I’ll go through each of them and explain their purpose. You should get a clearer picture that way.
Private Function GetItemCount(strZipFile)
GetItemCount = objShell.NameSpace(strZipFile).Items.Count
End Function
The GetItemCount function is used to populate the class object’s Count property. Since the Count property provides a real-time count of the files in the compressed folder, I needed to create a function that would calculate that value whenever the property was referenced. I’m simply returning to the Shell Object to get an item count on the folder. (This is a prime example of keeping my code inside of a function rather than processing it inside of the property block itself.)
Private Function ShellBusy()
intStartSize = objFso.GetFile(FullName).Size
Sleep 500
ShellBusy = objFso.GetFile(FullName).Size > intStartSize
End Function
As I mentioned briefly earlier, the Shell object’s move and copy procedures are performed synchronously. This means that the script will continue to execute while the process completes in the background. Since there are no easily available events, it becomes difficult to monitor the completion status and success of these actions.
To circumvent these shortcomings, I’ve created a bit of code that works to pause script execution until these actions complete—essentially, turning this into an asynchronous process. You can see this code in action in the AddFile Method. I’m basically monitoring the starting folder count and waiting until the count changes, indicating that the file has been successfully added.
This poses a slight problem. In the event of a copy error, the folder count would never change, thus the script would suspend indefinitely. So I needed a better solution.
The Shell object does not provide any means of monitoring its busy status. It also does not provide any events to indicate when a move or copy process completes. You could create a viable solution using WMI events (and possibly its move and copy methods, which I have not tested with compressed folders), but that would be a lot of very advanced code.
I decided to set a timeout limit on my loop so that it couldn’t cause the entire script to hang. In the meantime, I created the ShellBusy routine that attempts to determine whether the Shell object is still busy moving files around. It works in much the same way as my previous loop, only it uses the Shell object to monitor disk size for changes. Since the disk size value changes more frequently than the file count (during time-consuming processes, such as when moving large files), it provided a more accurate means of watching the processes. This method is slightly better than the first, but since neither of them is foolproof, I elected to use them together.
Private Sub Sleep(intDuration)
'You must provide an applicable sleep routine for use in ASP.
WScript.Sleep intDuration
End Sub
This simple Sleep routine is just a wrapper for a call to the WScript object’s own Sleep method. Since the WScript object is only available in WSH (and I wanted my class to be compatible with ASP, WSC, HTA, etc.), I used a wrapper function to allow developers to drop in a compatible sleep function without having to edit any of the previous methods.
Next: Wrapping up >>
More Code Examples Articles
More By Nilpo