Creating Simple Batch Program In Windows XP
The most important step in writing a batch program (or any other kind of computer program) is to state the problem correctly. In this example, the problem is to connect to a dialup connection by repeatedly dialing different connections until a successful connection is made. If a connection fails (because the line is busy or you enter the wrong password, for example), the program should try another dial-up connection.
This batch program relies on Rasdial.exe, the command-line version of the dialing component used by dial-up connections. The program uses dial-up connections that you have previously created in the Network Connections folder.
To create a batch program, use a plain-text editor, such as Notepad. (When you save the file, be sure to give it the .bat or .cmd file name extension.) The following listing shows Dial.bat:
@echo off
rem Connects to a Dial-up Connection
title Dialing Connections
:START
echo Connecting to MSN
rasdial msn pct_hiker *
if not errorlevel 1 goto end
echo Connecting to Earthlink
rasdial earthlink swdocs *
if not errorlevel 1 goto end
echo Connecting to local ISP
rasdial "pasadena isp" swdocs *
if errorlevel 1 goto start
:END
title Command Prompt
You execute a batch program by typing its name at the command prompt, like this:
C:\>dial
By default, batch programs display each line on the screen before attempting to execute it. Because this is seldom desirable, most batch programs start with the same line that ours starts with:
@echo off
The at sign (@) tells the program not to display the Echo Off command. The Echo Off command tells the program not to display any more lines for the rest of the batch program.
The line that follows the descriptive Rem statement
title Dialing Connections
sets the title of the Command Prompt window to Dialing Connections, as shown in Figure. This title helps orient users to the task at hand.
Figure. The Title command sets the title of the Command Prompt window.
Between the Start and End labels are three sets of dialing commands—one for each Internet service provider (ISP) for which we’ve created a dial-up connection. As we’ve used it here, the parameters following Rasdial are the connection name (the name that appears in the Network Connections folder) and the user name. The asterisk causes Rasdial to prompt for your password; you can put the password in your batch program (in plain text) if you’re sure that it won’t be compromised.
Like many programs, when Rasdial exits, it sets an error level, which is simply a numeric result code. The If statement in Dial.bat tests the error level for values of 1 or greater. An error level of 0 indicates a successful connection. (Rasdial uses many other values to indicate various error conditions. For a list of error codes, type hh netcfg.chm to open Network Connections help; on the Contents tab, open Troubleshooting and select Error Messages.)
Each of the If commands includes a Goto command that directs processing to the correct part of the batch program. When a Goto is executed, the command interpreter jumps to the line with the matching label and starts executing the lines it finds following the label. (A label is a line starting with a colon.) Following the first two connection attempts, if the error level value is not greater than or equal to 1, the command processor transfers control to the End label; otherwise, it drops down to the next statement. In the final If statement, the program jumps back up to the Start label if the error level is greater than or equal to 1.
The Goto End statement prevents the subsequent commands from being executed by directing processing to the End label. Without these statements, Rasdial would dial the second and third connections, even if the first one was successful.
The single statement that follows the End label simply resets the window title.


LinkBack URL
About LinkBacks
Reply With Quote

LinkBacks Enabled by vBSEO
Bookmarks