Posted by: Jerry Lees
advice, tips and tricks, VBScript, vbscript tips
In this installment I thought I’d give the network administrator looking to learn VBScript a little help in focusing in of key skills needed to write not only useful VBScripts, but help them make their lives easier! There are certainly more concepts that are helpful, but these are the ones I think are the most basic to everything else.
So let’s dive in with Number 10!
10. String Manipulation is your friend.
There’s a reason people are still writing scripts in PERL. It is very useful in manipulating strings– and sometimes you need to manipulate and use strings to get done what you need to get done. Built in String manipulation functions like Replace(), Instr(), Mid(), Ucase(), and Lcase() are invaluable, so commit their syntax to memory or use a script editor with intellisense that will help you with the syntax as you type.
9. Learning to create functions and subroutines will make your life easier.
The key to generating a script quickly is to use the code you’ve already worked hard on creating previously. Most of my scripts that I post here are smaller for a single purpose but, they are still useful if you take the heart of the script and wrap it in a function! You will find, over time, you’ll end up digging through your script repository to copy and paste pieces of code into another script. (I do.) Plus, it will save you time! Be Lazy! Don’t re-invent the wheel every time you sit down in front of the keyboard!
8. Key scripting objects were created for a reason– so you don’t have to create them yourself.
This is a continuation of the same concept. Don’t re-invent the wheel. There are already objects for specific functions, string manipulation for example– so don’t reinvent the replace() statement unless you have a really good reason to do so. If you need a little different functionality try to at least wrap it in a function.
7. Recursion in your code can be your best friend– or your worst enemy.
Recursion is where a section of code (generally a function) calls itself. This is invaluable! It saves a ton of lines of code because sometimes you do need to use the functionality of a function inside itself. (file and directory manipulations for example) However, be aware that recursion is dangerous because it poses the potential to create an infinite loop (which would be very bad if you were deleting files) and also can be somewhat hard to troubleshoot.
6. You will eventually need to debug that script, so comment your code and use Wscript.Echo to write to the screen what you are doing.
It’s inevitable, at some point a situation is going to arise that you didn’t think of 3 years ago when you wrote the script and your going to have to figure out why that script you wrote (and forgot about) has been dying for 6 months.
Comments are helpful in documenting how and why a script is doing something in a particular manner. I can’t tell you how many times I’ve went to the effort to rewrite a section of code to find out my thought wasn’t going to work because of something I figured out previously and didn’t document or didn’t because the code didn’t make sense.
Another useful tip is to use Wscript.Echo to output the key information the script is using, a variable in a For Next loop for example, or a string being passed into a function and the output of the function. You can always either comment them out when you’re done — or use a variable to hold a “debug state” and wrap the Wscript.Echo in a if statement, like so:
‘This is the debug “flag”, set it to 0 to turn off debugging output
‘… lots of code omitted
If debug = 1 then
Wscript.Echo “We’re cooking with Gas now!”
That’s enough for now, you’ll at least have something to consider. There’s more in the next Blog posting, so continue on over there!