When you're creating a script, rewriting code that you've used in different scripts wastes time and can introduce errors. However, opening script files and trying to find a key section of code can be frustrating and time-consuming, especially if you've written many scripts. A better approach is to develop a collection of code excerpts, or code snippets, from scripts you've already tested and debugged. With a code collection, you don't have to reinvent the wheel in each script or search through piles of scripts. By leveraging previously debugged code, you can create scripts quickly and accurately.
You can keep commonly used code snippets in a text document. With each code snippet, you can include useful information such as
- the task the code accomplishes
- the OSs (e.g., Windows 2000, Windows NT) on which you've tested the code
- optional command switches that you can use in the code
- utilities the code needs to run
- explanatory notes about how the code works or problems you had to overcome to get the code to work
You should include as much detail as you think you'll need to understand the code 6 months or a year from now. Listing 1 includes an excerpt from a sample code snippet from my code collection. You can find the entire code snippet in the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com).
One-Liners: An Added Benefit
Another benefit of maintaining a code collection is that you'll likely gather a group of what I call "one-liners." These one-liners won't cause tears of laughter to flow from people's eyes, but they might just cause people's eyes to open wide in amazement. Users, systems administrators, and bosses who aren't knowledgeable about scripting are often astonished that you can enter a command sequence at the console and instantly receive a result.
Listing 2 contains several practical one-liners from my code collection. (You can find even more sample one-liners in the Code Library on the Windows Scripting Solutions Web site.) I set up these one-liners for command-line usage so that I can simply copy the one-liner I want to use, then paste it into a command-prompt window. However, I can easily adapt these one-liners for use in a script. I just add a percent sign (%) to each iterator variable in the For commands. (When you use an iterator variable in a For command that you execute from a command prompt, you use one percent sign—e.g., %i. When you use an iterator variable in a For command in a script, you use a double percent sign—e.g., %%i.)
Start Your Collection
To start your code collection, review all your scripts to find code snippets, including one-liners, that you can reuse in scripts or at the command line. Compile those code snippets in a text document, and group the code by topics. Add any information that you think will be helpful when you later use that code.
You can use any text editor or word processing program (e.g., Microsoft Notepad, Microsoft WordPad, Microsoft Word) to create your code collection. However, if that text editor or word processing program has a spell checker, be careful that the spell checker doesn't automatically correct what it perceives as spelling errors and thereby break your code. In addition, if the text editor or word processing program has automatic-correcting options (e.g., Word's AutoCorrect and AutoFormat options), make sure that you disable those options because they can introduce trailing spaces, characters, and formats that can cause your code to fail.
After you've reviewed your scripts, keep an eye open for good code snippets in code that other people have written. For example, you might want to incorporate the code snippet and one-liners that I've provided into your collection. However, before you add someone else's code to your collection, make sure you first test the code on your system and debug it, if necessary.
Having your commonly used code snippets and one-liners in one location can help you save time. This practice can also prevent you from introducing errors into your code.