
- •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
Displaying messages and command-echoing.
Use echo [message] command to display messages. To create a blank line in a batch program add an open bracket or period immediate after the echo command with no space. Create test.bat batch program and type the commands as shown below. For the result refer to Figure 4.
Example 1.
echo There will be a blank line below
echo.
echo Above line is blank
echo[
echo The above line is also blank
Figure 4. Displaying messages and blank lines
As you can see the result is not clear. The interpreter executes each line in turn, starting with the first. The command echo There will be a blank line below appears in the command prompt as if you have typed it, the command is executed and it outputs the message There will be a blank line below in the next line. Then the next command in the batch program is executed, the command echo. appears in the command prompt, it is executed, and it outputs the blank line. And so on. Hence, the prompt displays each command before executing it.
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.
Let us add echo off command at the beginning of the test.bat and create test1.bat batch program. Type the commands as shown below. For the result refer to Figure 5.
Example 2.
echo off
echo There will be a blank line below
echo.
echo Above line is blank
echo[
echo The above line is also blank
Figure 5. Displaying messages and blank lines using echo off
Now commands are not echoed in the prompt, except echo off command itself. Is there any possibility to prevent this command from echoing? Yes, it is. The @ symbol at the start of the line prevents the prompt from displaying that command. So we can use @echo off command.
Example 3.
@echo off
echo There will be a blank line below
echo.
echo Above line is blank
echo[
echo The above line is also blank
For the result refer to Figure 6.
Figure 5. Displaying messages and blank lines using @echo off
Redirecting output from the command prompt
Normally, when working with Windows command prompt, output (or returned results) from commands that are executed can be viewed in the command prompt window. But say you want to save a copy of the output from the command that was executed. One way is to capture the output directly from the command prompt window to a file by using the redirection command.
To use the redirect command, just use the symbol > (press Shift + greater than arrow) and the file name at the end of the command you want to execute.
Example. Let us execute the dir command and redirect its output from the screen to the dirfile.txt. In the command prompt type dir > dirfile.txt and press Enter (refer to Figure 6). This will create the file called dirfile.txt in the current directory you are at, with the output results of dir command (refer to Figure 7). The contents of the dirfile.txt is shown on Figure 8.
Figure 6. Redirecting dir command output
.
Figure 7. Current directory with dirfile.txt
Figure 8. Contents of dirfile.txt
When using a single redirect command, it will always over-write (delete) the contents of the file (unless you specify a different file name) before writing any new data. If you want to append (concatenate) data to an existing file, use two redirection symbols as shown in the following example.
Example. Let us append a new string to test1.bat.
echo Hello world!>>test1.bat
Next check the result by displaying the contents of the file with type test1.bat (refer to Figure 9).
Figure 9. Appending to a file
Now try to execute the batch program. As shown on Figure 10, there is an error message (refer to Figure 10). Test1.bat is the batch program or a command file, and interpreter tries to execute each command in it. But “Hello” is not a command.
Figure 10. Error message
We have used echo Hello world!>>test1.bat command, which appends only output of the echo command. If we want to append the whole command, not the message “Hello world”, what command should we write?
At first, open the test1.bat file with Notepad and delete the “Hello world” string, save file and exit. Return to Command Prompt window and type the command as shown on Figure 11.
Figure 11. Appending a command to a batch program