Programming-Constructing a Batch file

Batch file
In MS-DOS, OS/2, and Windows, a batch file is a text file containing a series of commands intended to be executed by the command interpreter. When a batch file is run, the shell program (usually COMMAND.COM or cmd.exe) reads the file and executes its commands, normally line-by-line. Batch files are useful for running a sequence of executables automatically and are often used by system administrators to automate tedious processes.

For more details, see the Batch file document at Wikipedia.

A sample batch file
In a recent post titled Syntactic Analyzer built with Flex and YACC I used a simple batch file in the compilers construction paper. Why I did that? To avoid boiler plate work while executing the commands necessary to generate the syntactic analyzer executable.

In this post I'll present and comment in more details the content of such batch file.

The following table shows the content of the batch file. I also include the command line number and its respective comment in the table, so that it's easier to visualize what's going on.

# Code Comment
1 @Echo Off Turn off screen messages
2 ::cls A comment. Start with ::
3 :Start Declare the label Start
4 Echo. Print a blank line. See the dot after Echo.
5 Echo Batch file Menu Print the string "Batch file Menu"
6 Echo. Print a blank line
7 Echo 1 - MinGW Print the string "1 - MinGW"
8 Echo. Print a blank line
9 Echo 2 - SinAn Print the string "2 - SinAn"
10 Echo. Print a blank line
11 Echo 3 - Exit Print the string "3 - Exit"
12 Echo. Print a blank line
13 set choice= Declare the choice variable
14 set /p choice=Choice: See note 1.
15 if not '%choice%'=='' set choice=%choice:~0,1% See note 2.
16 if '%choice%'=='1' goto MinGW Go to label MinGW at line 22
17 if '%choice%'=='2' goto SinAn Go to label SinAn at line 25
18 if '%choice%'=='3' goto Exit Go to label Exit at line 37
19 Echo "%choice%" is not valid. Prints error if choice isn't 1, 2 or 3
20 Echo. Print a blank line
21 goto Start Go to label Start at line #3
22 :MinGW Declare the label MinGW
23 path=C:\MinGW\bin;%PATH% Rewrite path with new value. See note 3.
24 goto end Go to label end at line 39
25 :SinAn Declare the label SinAn
26 @Echo On Turn on screen messages
27 path=C:\MinGW\bin;%PATH% Rewrite path with new value
28 cls Clear the screen
29 CD CompCons Navigate to CompCons directory
30 flex -i lexan.lex Execute flex command
31 yacc sinan.yacc Execute yacc command
32 gcc yyerror.c main.c -osinan -lfl Execute gcc command
33 sinan <factorial.txt Execute sinan command. See note 4.
34 cd.. Navigate one directory above
35 @Echo Off Turn off screen messages
36 goto end Go to label end at line 39
37 :Exit Declare the label Exit
38 goto end Go to label end at line 39
39 :end Declare the label end. EOF :)

There are four notes that I think are worth commenting:

  1. set /p choice=Choice:
    The /p switch tells the command interpreter that the value of the variable choice will be the input entered by the user. The interpreter will display the string "Choice:" before reading the line of input.
    This command has the following format: set /p variable=[promptString]
  2. if not '%choice%'=='' set choice=%choice:~0,1%
    Every time you need to get the content of a variable it's necessary to enclose it with the percent sign %. Note the use of the quotation mark in both sides too. If a command contains two percent signs, the command interpreter will treat any characters between them as an environment variable to be expanded.
    The tilde sign ~ tells the interpreter that I want to slice a string in a variable.
    This command has the following format: %NAME:~s,n where s is the start position (zero-offset) and n is the number of characters. If s is negative, then the offset starts from the right side minus 1 (i.e. -1 refers to the rightmost character). If n is negative, then length -n characters, are extracted.
    If the variable choice isn't empty, get only the first character of the user input.
  3. path=C:\MinGW\bin;%PATH%
    The path variable is already part of the operating system set of variables. In this case I'm just rewriting its value with the desired directory. Note that I also attribute to path the content of other defined variable called PATH.
  4. sinan < factorial.txt
    Having no to do with the batch file explanation and just for the sake of clarification: the less angle bracket symbol < means that I'm redirecting the contents of the file factorial.txt to the executable file sinan. The sinan executable will consume the data stored in the factorial.txt file to do its computations.

To execute a batch file, just open a command prompt and type its name. You can even type the name without the .bat extension.

When a batch file is executed, the command interpreter will automatically execute each line of the batch file automating the whole process, that is, you won't need to type the same series of commands each time you want to execute a single work.

In this sample batch file what I want to accomplish is to run 4 executable files - see lines 30, 31, 32 and 33. By selecting option 2 at line 17, the interpreter goes to line 25 and so I'll have all the 4 executables running with a single command since the SinAn label at line 25 comprises these commands.

You can get the sample batch file at: