This introduction to scripting is intended to give you a basic understanding of how to create scripts using common features of the core Windows operating system. It covers basic command operation and how to use common shell programming fundamentals to pipe and redirect output and data.
More advanced concepts like intelligence, logic and programmatic loops are covered in the next section – Using intelligence.
To be successful in writing scripts, you will need to be intimately familiar with shell commands and their behaviour. A number of recursive operations depend on the ability to manipulate commands and file input to provide a specific format and/or sequence.
For example, the DIR command (when run from a command prompt) displays a list of files in the current directory as follows:
C:\> dir Volume in drive C is System Volume Serial Number is 50B5-F3BA Directory of C:\ 07/09/2003 01:35 AM <DIR> Documents and Settings 14/07/2004 13:07 PM <DIR> Inetpub 07/07/2004 15:37 PM 1,536 instmsi.log 17/11/2004 19:36 PM <DIR> Program Files 04/02/2004 16:39 PM <DIR> Temp 01/12/2004 10:15 AM <DIR> WINDOWS 1 File(s) 1,536 bytes 5 Dir(s) 507,412,480 bytes free C:\>_
The output provided is useful to look at, but not ideal for getting a simple list of files or directories. To do this, we need to run the DIR command with one or more switches. Switches provide us with the ability to alter the behaviour of a command. They do not apply to all commands and executables but most will have some. In this case, we want to use the /B switch as follows:
C:\>dir /b Documents and Settings Inetpub instmsi.log Program Files Temp WINDOWS C:\>_
In the output, only the names of the file and directory objects are shown. This is useful for listing files or directories or for doing recursive operations on either. If we only needed to list directories we could add another switch. /A (A for Attributes) allows us to specify a filter to apply to the list based on any of the file attributes. In this case we only want a list of directories, so we use /AD as follows:
C:\>dir /b /ad Documents and Settings Inetpub Program Files RECYCLER System Volume Information Temp WINDOWS C:\>_
Note:Be careful with this, because other attributes are ignored. You will notice that two new entries have appeared (RECYCLER and System Volume Information). These folders are normally hidden. If you need to exclude the hidden directories too, add a -H to the end of the switch as follows:
C:\>dir /b /ad-h Documents and Settings Inetpub Program Files Temp WINDOWS C:\>_
You can also use the ‘-‘ sign to NOT list the directories, so if you only wanted to list files, you would use /A-D as follows:
C:\>dir /b /a-d boot.ini instmsi.log IO.SYS MSDOS.SYS NTDETECT.COM ntldr pagefile.sys C:\>_
or without the hidden files:
C:\>dir /b /a-d-h instmsi.log C:\>_
As you can see, knowing how to use the commands effectively gives you the power to significantly change their behavior. The ability to do this is one of the basic requirements of successful scripting. It would be beneficial to learn the available system commands and their switches as many of them are needed in day to day tasks.
Next, we will have a look at using the output of commands to do other tasks.
Now that we know how to manipulate system commands and their output, we need to learn how to put it to good use. The two basic concepts we will cover here are PIPING and REDIRECTING.
We use piping to take the output of one command as the input to another. Not many commands or programs will accept input in this way, but in many of the basic commands it’s very useful. One function that is commonly used is the FIND command. FIND will accept piped input for processing and produce results based on the switches and parameters you use to call it.
Without piped input, the FIND command simply searches for string patterns in a file and displays the lines containing the pattern. Valid switches (taken from FIND /?) for the FIND command are:
- /V – Displays all lines NOT containing the specified string.
- /C – Displays only the count of lines containing the string.
- /N – Displays line numbers with the displayed lines.
- /I – Ignores the case of characters when searching for the string.
- /OFF[LINE] – Do not skip files with offline attribute set.
- “string” – Specifies the text string to find.
- [drive:][path]filename – Specifies a file or files to search.
If we wanted to find all lines containing the word Smith in a text file with names and numbers, we would use the command:
C:\>find /i "smith" phonebook.txt ---------- PHONEBOOK.TXT John Smith 555-2973 Mary Smith 555-7116 C:\>_
When using pipes, the filename parameter is excluded and the piped data is used as input instead. To do the same thing, we can use the TYPE command to produce the contents of the file and use it for input to the FIND command:
C:\>type phonebook.txt¦find /i "smith" John Smith 555-2973 Mary Smith 555-7116 C:\>_
Note the ‘ ¦ ‘ character before the find command. This is known as a PIPE. It’s also useful to note that when piping into FIND, the filename is omitted, so the output is a little more manageable. A more useful application of using a pipe would be to count the number of files in a directory or disk. We can use the DIR command piped to the FIND command with the /c switch:
C:\>dir /b /a-d¦find /i /c ".doc" 19 C:\>_
to count the number of Word documents or
C:\>dir /b /a-d *.doc¦find /i /c "2004" 5 C:\>_
to count the number of Word documents with ‘2004’ in the name. Note that there is often more than one way to achieve the same result. This can be good or bad depending on the circumstance, but it can sometimes be useful to have an alternative.
Now that we have some basics, the next step is to collect one or more commands and save them as a batch file. This allows us to run as many commands as we want by simply running or executing the file.
The format of the batch file is simple text. To create and edit batch files we use a text editor such as Notepad. The file extension given to batch files in Windows NT an above is .CMD. Prior to Windows NT, .BAT was used. BAT files are still used in Windows NT, but it is only preferable to do so when DOS or Windows 95/98 workstations will also be running them.
One of the useful things about batch files is the ability to pass them arguments. Up to 9 arguments can be specified, separated by spaces. Arguments are referenced within the batch file with the ‘%’ sign, with ‘%1’ being the first argument and ‘%9’ being the last.
To demonstrate, we can use the examples above. The batch file FCOUNT.CMD will contain:
dir /b /a-d *.%1¦find /i /c "%2"
To run the batch file, we would issue the command as follows:
C:\>fcount.cmd doc 2004 5 C:\>_
Notice that the file is being called with two arguments, ‘doc’ for the file type and ‘2004’ as a search string. This would have exactly the same effect as the example above, but now we can alter the the two arguments to change the query with greater ease.
There are many more things to know about batch file behavior, most of which will be covered later in this tutorial. For now, knowing these basics will allow us to start exploring their potential.
Redirection is used to change the output path from the command window to a file or device. Devices could be a printer, serial port or some other special device. One such device that is commonly used is the NUL device. NUL simply means ‘nothing’. We use it to discard output that’s not required.
Most of the time, we use redirection to write data to files. It is also useful to mention that redirection can occasionally be used for input to a command, but it is rare. To do redirection we use the ‘ > ‘ character for output and the ‘ < ‘ for input. When dealing with output to a file, the ‘ > ‘ will overwrite the file, so to append data to a file we use ‘ >> ‘.
Generally we use redirection in two functions, command output and echoed data. Command output is simply the command you want to run followed by the redirection character/s and the name of the file you want to write the output to:
dir /b *.doc >filelist.txt
Echoed data is simply some text or data that is written to the file by using the ECHO command:
echo List of Word documents>filelist.txt
Or, when used together:
echo List of Word documents>filelist.txt dir /b *.doc >>filelist.txt
The first command above will create (or overwrite) a file named ‘filelist.txt’ with the string ‘List of Word documents’ on the first line. The second command will append the output of the DIR command to the file.
Note: Redirection also has the ability to know the difference between normal output and error output. To make use of this feature, redirection can be called with the number 1 or 2 in front of the symbol, like 1> or 2>. The number 1 is used for normal output and 2 is used for error output. This is very useful for capturing problems with your script, for example:
echo List of Word documents>filelist.txt dir /b *.doc 1>>filelist.txt 2>>error.log
Would write the expected output to the filelist file or write the error output to the error.log file. Not all commands and programs are able to support this method, but most are. The only way you can tell is to test it.
The last basic concept in command scripting is the use of variables. All systems will have systems specific and user specific variables. To see a list, type SET at the command prompt. These system and user variables can be very useful for things like determining the operating system type and user home directory locations. The system will also allow the use of user specified variables. This is where batch scripting starts to become really useful.
Variables are set using the set command:
From the command line, we can reference this variable by surrounding the variable name in ‘%’ signs:
C:\>echo The variable is %myvar% The variable is December C:\>_
When using variables in command scripts, it is advisable to use variable manipulation locally, i.e. Variables can be set and referenced locally within the script, but not from outside the script.
To do this, we use the SETLOCAL command at the beginning of the batch file:
The ENABLEDELAYEDEXPANSION parameter allows the local variables to be manipulated repeatedly. This is useful for things like counting loops etc. and is always required in more advanced scripts. When using this feature, the variable is referenced with the ‘!’ instead of ‘%’:
set myvar=December echo The variable is !myvar!
System variables are always referenced with the ‘%’ sign, even when the ENABLEDELAYEDEXPANSION parameter is set. Some useful examples are:
C:\>echo Todays date is: %date% Todays date is: Sun 08/02/2004 C:\>echo My username is: %username% My username is: bob C:\>_
Effective use and manipulation of variables is an important component of successful scripting. Without this ability, we would not be able to achieve much.
Now that we have been introduced to some basic scripting concepts, we can move on to the next section ‘Using Intelligence’ where we will learn how to manipulate input, output and variables using built-in programming functions. It would be wise to review this section and become familiar with the basics before moving on.