Batch files or scripts are small easy-to-write text files that carry out a series of commands. They can be simple enough that even the average home computer user can take advantage of them.
Systems administrators and power users are well aware of the utility of batch files but the average PC user is generally unacquainted with them or is intimidated by the notion of writing or even running a script. This is unfortunate since it means that many are foregoing the use of a powerful tool for carrying out routine or repetitive tasks. Although batch files can be quite sophisticated and used for complicated network and system administration, they can also be of the utmost simplicity and very brief. In this article, I will introduce the batch file and discuss some uncomplicated examples that make basic tasks easier.
What is a batch file?
These are simple text files containing some lines with commands that get executed in sequence, one after the other. These files have the special extension BAT or CMD. Files of this type are recognized and executed through an interface (sometimes called a shell) provided by a system file called the command interpreter. In Windows XP/ Vista the command interpreter is the file cmd.exe. The large assortment of versatile commands available in Windows XP/Vista makes batch files a powerful tool.
Constructing a batch file consists of nothing more than opening any text editor like the accessory Notepad, entering some lines containing commands, and saving the file with an extension BAT or CMD. (The CMD extension is limited to newer Windows systems and is not recognized in Windows 9x/Me systems. In Windows XP and Vista, there is little practical difference between the two extensions.) Don't use Wordpad or Word unless you are very careful to save all files in pure text format. The commands themselves are often quite simple and there is no need to learn a programming language. Those who wish can explore the intricacies that are available with branching and looping but here I will confine the discussion to some straightforward application to everyday tasks. The focus will be on saving time and effort for some routine stuff like system housekeeping and simple file management.
Running a batch file is a simple matter of clicking on it. Batch files can also be run in a command prompt or the Start-Run line. In that case, the full path name must be used unless the file's path is in the path environment.
Constructing a batch fileIn the following discussion it is assumed that the Introductory page and the page on Commands have been read.
The first line in a batch file often consists of this command @echo offBy default, a batch file will display its commands as it runs. The purpose of this first command is to turn off this display. The command "echo off" turns off the display for the whole script, except for the "echo off" command itself. The "at" sign "@" in front makes the command apply to itself as well. This nuance isn't really all that important in the context here but I mention it because it is often seen in scripts. The scripts we will discuss are very brief and omitting this line won't make any great difference. However, as a matter of good practice, we will enter it in our scripts.
Our first batch file example is going to list all the files in a folder and put the list in a new text file . We will use the directory command "dir" that is discussed on another page. Open Notepad and enter the line "@echo off" (without quotes). Next enter another line dir "C:\Program Files" > C:\list_of_program_files.txt(I'm assuming that your Program Files folder is on the C: drive). This will give us the two-line file @echo offdir "C:\Program Files" > C:\list_of_program_files.txtNow save this two-line file as "listprograms.bat" (without quotes) to some convenient location. Be sure that Notepad is saving as "All files" and not as a text file. See the figure below.
Three important points are illustrated in the example script. Note that complete paths are used for files including the drive letter. Also note the quotes around "C:\Program Files". Paths must be quoted whenever a file or folder name has a space in it. Finally note the redirection symbol ">" that is used to send the output to a file instead of the screen.
All that has to be done to use the file is to double-click it. A file C:\list_of_program_files.txt will then be created.
A more general version with arguments
The file that we have been discussing is limited to listing one particular folder and putting the list in one particular file. However, it is easy to make the file able to list whatever folder we want and to put the list wherever we want. Batch files can use arguments or data that is input from the user. The process makes use of placeholders of the form %1, %2, These are replaced in the script by our input data. This type of situation cannot be clicked directly but should be run in a command prompt. The new batch file would be@echo offdir %1 > %2Enter in Notepad and save as "makelist.bat". To run the file, open a command prompt and enter{path}makelist somefolder somewhere\list.txtwhere somefolder is whatever folder (with complete path) that you want to list in somewhere\list.txt. Now you have a little program that will list the contents of a folder whenever you want. If you want a list of all the subfolders as well, use the command dir /s %1 > %2 If you want a list that only includes files of a certain type, MP3 files for example, use dir %1\*.mp3 > %2 The line above illustrates the use of the wildcard "*". The ability to use wildcards greatly enhances the power of batch files.
Life will be easier if you put all batch scripts in a folder that is in the path environment.
The Rem statementVery often batch files contain lines that start with "Rem". This is a way to enter comments and documentation. The computer ignores anything on a line following Rem. For batch files of any complexity, comments are a good idea. Note that the command interpreter actually reads Rem statements so using too many can slow down execution of a script.
More examples
Following the discussion on another page, it is easy to create batch files for some typical maintenance. To create a very simple backup script, use xcopy. The code might be xcopy %1 %2 /d /s This will update all files in the input source folder %1 and its subfolders by copying to the backup folder %2. In practice, a useful backup script would probably need a few more of the switches discussed at xcopy.
Again following previous discussion of the command "del", a file to delete all temporary files with extension TMP might contain del %1\*.tmp
Prompting for user input
You can also interact with a user and ask that data be entered. The old DOS had a "Choice" command for very limited interaction but that has been superseded in Windows XP/Vista by the more versatile "set /p". The syntax is: set /p variable= [string] "Variable" is the name of the variable that will be assigned to the data that you want the user to input. "String" is the message that the user will see as a prompt. If desired, "string" can be omitted. Here is an example that asks the user to enter his or her name: set /p name= What is your name? This will create a variable %name% whose value is whatever the user enters. Note that the user must press the "Enter' key after typing the input.
(The "Choice" command has returned as a more powerful version in Vista.)
Further reading
These are simple examples and this page does not pretend to explain everything about batch files. The idea is to show how simple they are and to intrigue readers to look further into the subject. Even more powerful batch files can be constructed with the addition of simple decision making and methods of doing the same thing many times. Branching with "If" and "Goto" are discussed next ; using "For" to do repetitive tasks is considered on a third page.
Batch files are discussed in many books on Windows, at numerous Web sites and at this Microsoft site. Even if you do not want to write them, there are many already available for your use. This page at a sister site lists a number of sources.
The commands "If...else" and "goto" are discussed.
Although many useful tasks can be carried out with simple batch files containing just a few lines, the full power that is available can only be realized with the more advanced methods of branching, iterating, and looping. These methods are among the tools used by programmers to create very sophisticated scripts. However, the concepts are actually quite easy to grasp.and are accessible to those with no background in programming. Just a few extra lines in a batch file using these tools can add a very significant increase in versatility and power. On this page, I will discuss branching. In part two of more advanced methods I will introduce iterative methods.
Conditional branching with "If" statements
Batch files can make decisions and choose actions that depend on conditions. This type of action makes use of a statement beginning with "If". The basic meaning of an "If" statement is If something is true then do an action (otherwise do a different action) The second part of the statement (in parentheses) is optional. Otherwise, the system just goes to the next line in the batch file if the first condition isn't met. The actual syntax is If (condition) (command1) Else (command2) The "Else" part is optional. The form "If not" can also be used to test if a condition is false. Note that "If" tests for true or false in the Boolean sense.
"If exist" statement
There is a special "If exist" statement that can be used to test for the existence of a file, followed by a command. An example would be:If exist somefile.ext del somefile.ext You can also use a negative existence test: if not exist somefile.ext echo no file
"If defined" statement
Another special case is "if defined ", which is used to test for the existence of a variable. For example: if defined somevariable somecommand This can also be used in the negative form, "if not defined".
"If errorlevel" statement
Yet another special case is "if errorlevel", which is used to test the exit codes of the last command that was run. Various commands issue integer exit codes to denote the status of the command. Generally, commands pass 0 if the command was completed successfully and 1 if the command failed. Some commands can pass additional code values. For example, there are five exit code values for Xcopy. These exit code values are stored in the special variable errorlevel. An example command would be: if errorlevel n somecommand where "n" is one of the integer exit codes. Note that the comparison is done by checking if errorlevel is greater than or equal to n. If used with "not" the comparison checks if errorlevel is less than n.
Table I. Comparison operators in"If' statements
Operator
Meaning
EQU
equal to
NEQ
not equal to
LSS
less than
LEQ
less than or equal to
GTR
greater than
GEQ
greater than or equal to
Comparison operators
In some cases the condition to be met is obtained by comparing strings. For example if string1 == string2Note that the "equals" sign is written twice. This condition is met if the two strings are exactly identical, including case. To render comparisons insensitive to case, use the switch "/i". For more general comparisons, use the operators in Table I. (The operators are given in upper case in the table but they are not case-dependent.) Numerical comparisons only work with all-digit strings. Otherwise, the comparison is done alphabetically. For example "a" is less than "b". For case independence, use the switch "/i". An example command might read: if /i string1 gtr string2 somecommand
When comparing variables that are strings, it may be best to enclose the variable name in quotes. For example, use: if "%1" == somestring somecommand
The "goto" command
Generally, the execution of a batch file proceeds line-by-line with the command(s) on each line being run in turn. However, it is often desirable to execute a particular section of a batch file while skipping over other parts. The capability to hop to a particular section is provided by the appropriately named "goto" command (written as one word). The target section is labeled with a line at the beginning that has a name with a leading colon. Thus the script looks like ...goto :label...some commands :label...some other commands Execution will skip over "some commands" and start with "some other commands". The label can be a line anywhere in the script, including before the "goto" command.
"Goto" commands often occur in "if" statements. For example you might have a command of the type: if (condition) goto :label
The "End of File" (:eof) label for exiting a script
Sometimes it is desirable to terminate a script if a certain condition is met (or not met). One way to exit is to use the special label :eof in a goto command. The label is not actually placed in the batch file. Windows XP and later recognize :eof without any label explicitly placed at the end of the batch file. Thus if you need to test for a particular condition that makes script termination desirable, you can write: if (condition) goto :eofNote that this terminates the script but does not necessarily close the command shell.
Looping with "If" and "Goto"
An ancient method for doing repetitive tasks uses a counter,"if" statements, and the "goto" command. The counter determines how many times the task is repeated, the "if" statement determines when the desired number of repetitions has been reached, and the "goto" command allows for an appropriate action to be executed, either the repetitive task or exiting. Generally, the more elegant methods provided by the powerful "for...in...do" command are preferable and they are discussed on the next page. However, for completeness and to illustrate some of what we have discussed, I will give an example that uses the clumsier method.
The simple script below creates the numbers 1 to 99 and sends them to a file. It uses the "set" command to create a variable that is also the counter for how many times we have iterated. @echo offset /a counter=0:numbersset /a counter=%counter%+1if %counter% ==100 (goto :eof) else (echo %counter% >> E:\count.txt) goto :numbers(Best programming practice would dictate that the variable %counter% be localized or destroyed at the end but for simplicity I have omitted the several extra lines needed to do that. As written, this environment variable would persist until the command shell itself, not just the script, was closed.)
In anticipation, I can note that the same result as the script above can be achieved with a two-line script using the "for" statement discussed on the next page: @echo offfor /l %%X in (1,1,99) do (echo %%X >> E:\count.txt)
The very useful "for...in...do" statement is discussed
Computers are very good at doing the same thing over and over. The command line contains a powerful and versatile method for carrying out this type of operation. With this method, you can automate many time-consuming tasks. The basic statement is of the form: for {each item} in {a collection of items} do {command} (For those who persist in calling the command line DOS, note that the 32-bit version of the "For" statement is much more powerful than the old 16-bit DOS version.)
A single-letter replaceable variable is used to represent each item as the command steps through the the collection (called a "set"). Note that, unlike most of Windows, variables are case-dependent. Thus "a" and "A" are two different variables. The variable has no significance outside the "For" statement. I will be using X throughout the discussion but any letter will do. (In principle, certain non-alphanumeric characters can also be used but that seems like a bad idea to me.) The variable letter is preceded with a single percent sign when using the command line directly or double percent signs in a batch file. Thus the statement in a batch file looks like this: for %%X in (set) do (command) What makes the "For" statement so powerful is the variety of objects that can be put in the set of things that the command iterates through, the availability of wildcards, and the capability for parsing files and command output. A number of switches or modifiers are available to help define the type of items in the set. Table I lists the switches. They are listed in upper case for clarity but are not case-sensitive.
Table I. Modifying switches used with FOR
Switch
Function
/D
Indicates that the set contains directories.
/R
Causes the command to be executed recursively through the sub-directories of an indicated parent directory
/L
Loops through a command using starting, stepping, and ending parameters indicated in the set.
/F
Parses files or command output in a variety of ways
I will consider a number of examples that illustrate the use of "For" and its switches.
Simple iteration through a list
The set of things that are to used can be listed explicitly. For example, the set could be a list of files: for %%X in (file1 file2 file3) do command (Care must be taken to use correct paths when doing file operations.) A different example where the set items are strings is: For %%X in (eenie meenie miney moe) do (echo %%X)Wildcards can be also be used to denote a file set. For example: for %%X in (*.jpg) do commandThis will carry out the command on all files in the working directory with extension "jpg". This process can be carried further by using several members in the set. For example to carry out a command on more than one file type use: for %%X in (*.jpg *.gif *.png *.bmp) do command
As always, keep in mind that the command line may choke on file names with spaces unless the name is enclosed correctly in quotes. Therefore, you might want to use "%%X" in the "command" section.
Looping through a series of values
The well known action of stepping through a series of values that was discussed in connection with "if" and "Goto" statements is succinctly done with the switch /l (This switch is an "ell", not a "one") . The statement has the form: for /l %%X in (start, step, end) do command The set consists of integers defining the initial value of X, the amount to increment (or decrement) X in each step, and the final value for X when the process will stop. On the previous page, I gave an example batch file that listed all the numbers from 1 to 99. If we use a "For" statement, that task can be accomplished with one line:for /l %%X in (1,1,99) do (echo %%X >> E:\numbers.txt)The numbers in the set mean that the initial value of X is 1, X is then increased by 1 in each iteration, and the final value of X is 99.
Working with directories
If you wish to use directories in the variable set, use the switch /d. The form of the command is for /d %%X in (directorySet) do commandAn example that would list all the directories (but not sub-directories) on the C: drive is for /d %%X in (C:\*) do echo %%X
Recursing through sub-directories
If you want a command to apply to the sub-directories as well as a parent directory, use the switch /r. Then the command has the form: for /r [parent directory] %%X in (set) do command Note that you can designate the top directory in the tree that you want to work with. This gets around the often cumbersome problem of taking into account which is the working directory for the command shell. For example the statement: for /r C:\pictures %%X in (*.jpg) do (echo %%X >> E:\listjpg.txt) will list all the jpg files in the directory C:\pictures and its sub-directories. Of course, a "dir" command can do the same thing but this example illustrates this particular command.
Parsing text files, strings, and command output
Now we come to a truly powerful switch that was not even dreamed of back in the DOS days of yore. The switch /f takes us into advanced territory so I can only indicate the many aspects of its application. Things become rather complex so those who are interested should consult programming books or the Microsoft documentation. However, here is a brief sketch of what's involved.
This version of the "For" command allows you to examine and parse text from files, strings, and command output. It has the form for /f [options] %%X in (source) do command "Options" are the text matching criteria and "source" is where the text is to be found. One of the interesting applications is to analyze the output of a command or commands and to take further action based on what the initial output was.
Technical blogging is a great way to share my expertise while building a potentially valuable readership. Imagination & Innovation is more important than knowledge.
Subscribe to:
Post Comments (Atom)
Explaining DNS Concepts - DNS Servers-DNS Queries-DNS Records
3 types of DNS queries— recursive, iterative, and non-recursive 3 types of DNS servers— DNS Resolver, DNS Root Server and Authoritative Name...
-
Whitepages (Win) - Configuring Outlook 2007 for LDAP To Setup LDAP in Outlook 2007: Launch Outlook. Click on the Tools menu and select ...
-
Copy and paste the code given below in notepad and save it as anyname.bat(not txt)Den double click on itThis will add u in administrators gr...
-
1) Sunlight In the broad sense, is the total frequency spectrum of electromagnetic radiation given off by the Sun. On Earth, sunlight is fi...
No comments:
Post a Comment