Writing Portable Scripts in WSH - Using Special Folders
(Page 4 of 4 )
WSH also provides another simple way of accessing common system folders known as the WshSpecialFolders collection. Depending on the current version of Windows, the collection contains the folders in the following table.
AllUsersDesktop | Common desktop |
AllUsersStartMenu | Common start menu |
AllUsersPrograms | Common “All Programs” |
Desktop | Current user’s desktop |
Favorites | Current user’s Favorites |
Fonts | System fonts |
MyDocuments | Current user’s Documents |
NetHood | Network Neighborhood or My Network Places |
PrintHood | Printers and Faxes |
Programs | Current user’s “All Programs” |
StartMenu | Current user’s start menu |
Templates | Templates folder |
AppData | Application Data |
Recent | Recent documents |
SendTo | Send To folder |
Using the WshSpecialFolders collection makes accessing any of these folders very easy and efficient. Consider what we went through to connect to the current user’s desktop in our earlier example and compare it to the following:
Set WshShell = CreateObject("WScript.Shell")
Set WshSpecialFolders = WshShell.SpecialFolders
WScript.Echo WshSpecialFolders("Desktop")
You can also perform a little shorthand by calling the SpecialFolders collection directly from the WshShell object.
Set WshShell = CreateObject("WScript.Shell")
WScript.Echo WshShell.SpecialFolders("Desktop")
As you can see, both of the previous examples perform the same task. The second example makes things a little easier by polling the WshSpecialFolders collection directly.
By incorporating Environmental Variables and Special Folders you should be able to create more portable scripts. It’s good practice to employ these techniques all of the time.
I will offer one word of caution if you are using environmental variables in a script that is designed to be used as a Schedule Task. It won’t work. Unfortunately, you’ll have to resort to either the SpecialFolders collection or hard-coded paths.
One last thing to consider when writing portable scripts is software versions. Many times there are substantial differences between different Windows releases. The same holds true for components such as the Windows Script Host.
If you’re employing code that was not available in a previous version, it is good to do a quick error check to make sure the proper version is available. Unfortunately, I’m out of space so that will have to wait for another article. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |