DESKTOP

Windows Server 2008 : Creating Batch Files (part 2)

4/9/2013 3:56:08 AM

4. Clearing the Screen with cls

The cls command is useful within a batch file to clear the screen. It is entered simply as cls without any parameters. When entered, it clears the screen and puts the cursor at the top of the screen.

5. Calling Another Batch File with call

You can launch a batch file from within another batch file with the call command. The basic syntax is

call batchFileName

Figure 1 shows the order of processing when calling a batch file. Calltest.bat runs until it comes to the call calledbatch.bat line. It stops running and control is then passed to the called batch file, calledbatch.bat. After the called batch file runs, control passes back to the calling batch file.

Figure 1. Order of processing when calling a batch file


Figure 2 shows the output when the batch file is run from the command prompt and calls the second batch file.

Figure 2. Output of batch file calling another batch file

Note

If the called batch file doesn’t exist or somehow fails, control is still returned to the calling batch file. The call command works only from within a batch file. If you execute it directly from the command prompt, it is ignored.


The following table shows some use of the call command within a batch file.

Using the call CommandComments
call newbatch.bat

Calls the newbatch.bat batch file. The file must exist in the current path or in a path known by the system.
call c:\scripts\newbatch.bat

Calls the newbatch.bat batch file located in the c:\scripts folder. If the batch file doesn’t exist, the call fails and control returns to the calling batch file.
call newbatch.bat batch parameters

If the called batch program accepts parameters, you can include them in the call.
call newbatch.bat :Label

Calls the batch program and starts execution at the specified label.

6. Changing the Order of Processing with goto

You can use the goto statement to change the order of processing of a batch file. Normally, a batch file goes through the lines in the batch file from the first line to the last line. However, there are times you want to skip some areas of the batch file. The basic syntax of the goto statement in the batch file is

goto label

The label is listed elsewhere in the document as a string of characters beginning with a colon (:). For example, you can have a label at the end of the batch file named :eof. You can then include the following lines in the batch file:

Using the goto CommandComments
goto eof

The next command is at the :eof label. Notice that the label includes the colon (:), as in :eof, but the goto statement does not include the colon.
repadmin /syncall

This command never executes in this batch file because the goto eof statement always causes it to be skipped.
:eofThis is the eof label.
echo Exiting batch fileThe echo command gives the user feedback.

7. Checking Conditions with if

When creating a batch file, you might occasionally want to check for specific conditions. If a condition exists, you do something. You use if statements frequently with goto statements.

The basic syntax of the command is

If condition  command [else  command ]

As a simple example, you can use the following if statement in a batch file:

if "%date%" == "Tue 11/20/2012" call magic.bat else Echo "Not yet"

The condition of the if command evaluates to either true or false. If the condition is true, it performs the command. If the condition is false, it does not perform the command. If an else clause is included, it is performed if the condition is false. In this example, the condition is a check to see whether today (%date%) is Tuesday, Nov 20, 2012. If so, it calls a batch file named magic.bat.

Although you can enter the entire if command on the same line, it is sometimes easier to read and understand when entered on separate lines. However, if you’re using multiple lines, you must use parentheses around certain areas.

if condition (
  command
) else (
  command )

For example, the if command shown earlier would look like this:

if "%date%" == "Tue 11/16/2010" (
   call magic.bat
) else (
   echo "Not yet"
   )

There are certain conditions you can use, as shown in the following table.

ConditionExampleComments
Exist
if exist log.txt copy
log.txt archive.txt

Checks for the existence of the log.txt file and if it is there, it copies it to the archive.txt file.
Errorlevel
if errorlevel 4 goto eof
This assumes a label of
:eof exists in the batch
file.

Checks for the value of the %errorlevel% variable. If the value is 4, the batch goes to the eof label and presumably ends. It’s common to use this check to redirect the order of the batch file processing with the goto statement.
string comparison
string1==string2

if %1 == Yes echo "User
entered Yes"

Checks the value of one string against another. Notice that two equal signs (==) are required.
not
if not exist log.txt copy
log.txt archive.txt
if not errorlevel 4 goto
end
if not %1 == Yes echo
"User did not enter Yes"

The not condition negates the result of the conditional check. If the check results in true, the not condition changes it to false. If the check results in false, the not condition changes it to true.

Note

You can use any valid command that you want after the condition. In other words, you aren’t limited to using only certain commands when you use certain conditions. If it’s a valid command from the command line, it’s a valid command in the if statement.


When using comparison operators, you have several to choose from, as shown in the following table.

Comparison OperatorRemarks
==
if string1 == string2
if abc ==  abc
if %1 == abc
if %1 == %myvar%

Evaluates to true if string1 and string2 are the same. These values can be literal strings or batch variables, such as %1 or %myvar%. You do not need to enclose literal strings in quotation marks. For example, you can check to see what the user entered as a parameter using %1, and even check to see whether it’s equal to a variable you created with a statement such as set myvar= abc.
if /i string1 ==string2

If you want to ignore the case of the two strings, use the /i switch immediately after the if statement.
equ
if value1 equ value2

Equal to. Evaluates to true if the two values are equal.
neq
if value1 neq value2

Not equal to. Evaluates to true if the two values are not equal.
lss
if value1 lss value2

Less than. Evaluates to true if value1 is less than value2.
leq
if value1 leq value2

Less than or equal to. Evaluates to true if value1 is less than or equal to value2.
gtr
if value1 gtr value2

Greater than. Evaluates to true if value1 is greater than value2.
geq
if value1 geq value2

Greater than or equal to. Evaluates to true if value1 is greater than or equal to value2.

It’s common to use if statements in combination with goto statements and labels. For example, the batch file can accept an input and then check for the value of the input. Based on the input, it can take one of several actions. The following sample shows how to do this accepting a single parameter as %1:

@echo off
if %1#==# goto null
if %1==1 goto sync
if %1==2 goto show
if %1==3 goto summary
if %1 gtr 3 goto outofrange
:Null
echo "No Command entered."
goto eof
:outofrange
echo "Only values 1, 2, and 3 are valid."
goto eof
:sync
repdmin /syncall
:show
repadmin /showrepl
:summary
repadmin /replsummary
:eof

You can enter all of these commands in a batch file named repl.bat. You can then start the batch file with repl 1 (to run repladmin /syncall), repl 2 (to run repadmin /showrepl), or repl 3 (to run repadmin /replsummary).

For clarification, the commands used in the previous batch file are explained in the following table.

Note

Even though the example shows only one command in each of the labels, you can have as many commands as you like.


Batch File CommandComments
@echo off

Turns off echo so that the batch file commands don’t show.
if %1#==# goto null

Checks for a null value. In other words, if the batch file is entered without a parameter, this evaluates to true and the batch file goes to the :null label.
if %1==1 goto sync
if %1==2 goto show
if %1==3 goto summary
if %1 gtr 3 goto outofrange

Checks for the value of the input. If it is any number, it uses the goto statement to process the command.
:null
echo "No Command entered."
goto eof

The :null label provides feedback if a parameter isn’t provided. It gives feedback to the user and goes to the end of the file.
:outofrange
echo "Only values 1, 2, and 3
are valid."
goto eof

The :outofrange label provides feedback on valid numbers. If a value greater than 3 is entered, it gives feedback to the user and goes to the end of the file.
:sync
repdmin /syncall

The :sync label includes the repadmin /syncall command to force replication of objects between domain controllers (including those in different sites).
:show
repadmin /showrepl

The :show label includes the repadmin /showrepl command, which shows key information on replication.
:summary
repadmin /replsummary

The :summary label includes the repadmin /replsummary command, which includes a summary of replication events.
:eof

The :eof file label is placed at the end of the file and doesn’t have any commands after it. The batch file then exits.
Other  
 
Top 10
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
OPEL MERIVA : Making a grand entrance
FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
BMW 650i COUPE : Sexy retooling of BMW's 6-series
BMW 120d; M135i - Finely tuned
PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS