Downloads
16074.zip

Using Callback Functions with VBScript Classes

\[Editor's Note: Email your scripting solutions (400 words or less) to Reader to Reader at winscriptsol@win2000mag.com. Please include your script and phone number. We edit submissions for style, grammar, and length. If we print your contribution, you receive $100.\]

I often use VBScript code to browse through files and folders, so I wrote a VBScript class called HandleFolder. However, I soon discovered that I needed to execute code after I found the desired file or folder, so I had to find a way to implement a function without editing the class.

My solution was to add a fixed-name callback function called MyRecurseFolder to the HandleFolder class. I implemented this solution in the class. Listing 1 contains an example of how you use this fixed-name callback function to display the files and folders in the C:\temp directory.

Because a script can implement only one fixed-name callback function, my VBScript class still had limited usability. So, I added a second callback function in which the user chooses the name of the callback function (i.e., the function name isn't fixed). Using different function names lets the VBScript class reference multiple callback functions. Listing 2 contains an example of how you can use multiple callback functions first to display only the files, then to display only the folders in the C:\temp directory. In this example, I named the second callback function RecurseFolderEx.

In addition to the RecurseFolder() and RecurseFolderEx() callback functions, the HandleFolder class contains a third function: GetDriveSpaceInfo(). This function retrieves four properties related to drive space: nFreeSpace, nUsedSpace, nTotalSize, and nPercentageUsed.

You can find the HandleFolder class in the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com). The VBScript file Class_HandleFolder.vbs contains the original class code. The text file HandleFolder.txt contains an English translation of the class code. Both files include the MyRecurseFolder, RecurseFolderEx, and GetDriveSpaceInfo() functions.