Let's review a few general tips for scripting in VBScript. Future columns will include more specific tips that apply to the topic I discuss in the column, but these general tips will always apply.
- Keep the lines in your scripts short. Short lines are easier to read. VBScript lets you use underscores to break lines, and statements such as If ...Then ... Else let you list conditions instead of crowding everything on one line.
- Comment liberally. You know now what the statement you typed means. But will you remember what it means 6 months from now? Will someone else reviewing your code understand it? Make sure by spelling out what the pieces of the script do.
- Mix case in your code. The mixed-case oDiskSpace is easier to read than lowercase odiskspace or uppercase ODISKSPACE. VBScript is case insensitive for most purposes (one exception is when you're computing the ASCII character values for letters: Uppercase letters have ASCII values different from lowercase letters).
- When using Windows Script Host (WSH), use the command-line environment. WSH has two execution environments: a command-line one and a graphical one (the default). In the command-line environment, output appears in a command window unless you specifically send it to a dialog box. In the graphical environment, all output goes to message boxes. You'll want to use the command-line environment most of the time. Some operations won't work in a graphical environment, and if your script produces several lines of output, each line will have its own message box that will halt the script's execution until you click OK. To execute scripts in the command-line environment, you have two options: Preface each command you run with cscript, for example
or make the command line the default execution environment by typing
wscript //h:cscript //s