- •Laboratory work activity 1. Understanding and creating Batch programs.
- •Creating a batch program.
- •Displaying messages and command-echoing.
- •In order to turn command-echoing on or off use echo [on | off] command. Command echo off turns off the prompt permanently, or until it is turned on again by echo on command.
- •Redirecting output from the command prompt
- •Using command line arguments
- •Figure 12. Using parameters
Laboratory work activity 1. Understanding and creating Batch programs.
A command interpreter (also called a command line shell, command language interpreter, or abbreviated as CLI) is the part of a computer operating system that understands and executes commands that are entered interactively by a human being or from a program. A command-line interpreter reads lines of text entered by a user and interprets them in the context of a given operating system or programming language. This requires the user to know the names of the commands and their parameters, and the syntax of the language that is interpreted.
Batch programs are programs that allow MS Windows users to create a list of commands and/or programs to run once the batch program has been executed. Batch program is a text file containing a series of commands to be executed by the command interpreter. A batch program may contain any command the interpreter accepts interactively at the command prompt. A batch program may also have constructs (IF, GOTO, Labels, CALL, etc.) that enable conditional branching and looping within the batch program. When a batch program is run, the shell program (usually COMMAND.COM or cmd.exe) reads the program and executes its commands, normally line-by-line. The filename extension .bat or .cmd is used.
Creating a batch program.
Let us create our first batch program. This example batch program clears screen and then displays a list of files and subdirectories in the current directory (refer to Figure 1).
STEP 1.To create a basic batch program first.bat follow the below steps:
-
Creating a file from the console. Choose Start>All programs>Accessories>Command prompt or choose Start and in the Search programs and files field type cmd.exe, then press Enter.
In command prompt type
copy con first.bat
and press Enter. The data from the keyboard (CON device) will be copied to first.bat file. Now type the following commands
cls
dir
To end the file press F6 or CTRL+Z combination.

Figure 1. Creating the batch file
-
Create a file using any text editor, such as Notepad or Wordpad, as long as the file extension ends with .bat or .cmd. Choose Start>All programs>Accessories>Notepad or choose Start and in the Search programs and files field type notepad, then press Enter. Once Notepad is open, type the commands line by line. Click File>Save as…; browse to where you want to save the file. For the file name type “first.bat”, and in the Save as type field choose All files, otherwise it will save as a text file. Exit Notepad.
STEP 2.To displays the contents of a file use type<file_name> command (refer to Figure 2).

Figure 2. Displaying contents of the file
STEP 3. To run the batch program just type its name in the command prompt (refer to Figure 3). In Windows, if the batch program is run within a Command Prompt window, the window remains open at the prompt as in MS-DOS, otherwise the command prompt window closes on termination (unless the batch program has a command to prevent this).

Figure 3. Executing batch program
