Batch Files in DOS |
|
How does a batch file work? |
|
Now, consider the following instructions |
md \newdir copy \dos\*.exe \newdir cd \newdir dir cd \ |
|
|
Executing commands at DOS prompt: |
Normally, we can execute only one MS-DOS command at one time (except we use a "trick"). We cannot give another instruction before DOS has done our current command. |
|
If we manually instruct DOS to execute the above commands, we have to type each command at the DOS prompt one after another. |
|
|
Executing commands in a batch file: |
However, if we put all of the commands in a text file in the same manner as in the above box, it becomes an executable program. |
|
Let's name it anyname.bat Similar to a COM or EXE command, we can simply type the name of this batch file at the DOS prompt to start our instructions. |
|
i.e. C:\>anyname or C:\>anyname.bat (note: the extension bat is optional here. It makes no difference, no matter we put it or not.) |
|
DOS will then execute the commands automatically in the same order as written in the anyname.bat The followings are details of what DOS will do for us:- |
1. Creates a new directory under the root called newdir |
2. Copies all files under the DOS directory with an extension of EXE to the newly created newdir directory. |
3. Changes the current directory to newdir directory |
4. Display the directory listing of newdir directory |
5. Changes the current directory to root directory |
|
|
|
|
Batch Files in DOS |
|
What commands can we used in a batch file? |
|
All commands that we can use at the DOS prompt can be used in a batch file. We have to follow the same syntax of the commands as we use it at the DOS prompt. |
|
For advanced application of batch programs, the following nine (9) special commands are commonly used. Some of them are used together with special characters. |
|
CALL | FOR | PAUSE |
CHOICE * | GOTO | REM |
ECHO | IF | SHIFT |
|
CHOICE is an external DOS command. |
|
|
Note:- |
Please ensure the current path statement contains C:\DOS when we need to run a batch program in which the CHOICE command is used. |
|
Brief Description of MS-DOS Batch Commands |
CALL | Calls one batch program from another. |
|
CHOICE | Waits for the user to choose one of a set of choices. |
|
ECHO | Displays messages, or turns command-echoing on or off. |
|
FOR | Runs a specified command for each file in a set of files. |
|
GOTO | Directs MS-DOS to a labelled line in a batch program. |
|
IF | Performs conditional processing in batch programs. |
|
PAUSE | Suspends processing of a batch program and displays the message "Press any key to continue...." |
|
REM | comments (remarks) in a batch file or CONFIG.SYS. |
|
SHIFT | Changes the position of replaceable parameters in a batch file. |
| |
|
|
|
Batch Files in DOS |
|
What are the special characters commonly used in a batch file? |
|
|
|
Batch display-suppression operator |
o @ can be applied to any command as the first character on the command line in a batch program. |
o It's used to prevent an individual command from being displayed when it is executed. |
|
|
Batch file label operator |
o : is used to identify a location in a batch file. It is not a command but a prefix of a label. |
|
o A label is a companion of the GOTO command. |
i.e. GOTO <label> :<label> |
|
o A label is used to force MS-DOS to move to a specified location within the batch program. |
|
o Normally, when MS-DOS runs a batch program, it executes commands in the order as they appear in the program. However, when MS-DOS comes across the GOTO command(e.g. GOTO option1), it will move to the position that is identified by :option1 within the batch program. |
|
|
Batch replaceable parameter |
o Consider the following commands frequently used at the DOS prompt:- |
|
? copy autoexec.bat autoexec.bak |
This command line creates a backup file of the autoexec.bat file. |
copy is a command, autoexec.bat and autoexec.bak are parameters. These are required parameters. You must state both of them to make the command work. |
|
|
? dir /w |
This command line displays a directory entries of the current directory in wide screen format. dir is a command, /w is a parameter. It is an optional parameter for qualifying the output of the dir command. |
|
o Similarly, we can make use of parameters to help us achieve the same capability in executing a batch program. This way, we can execute the same batch program with different data at different time. They are called replaceable parameters. |
|
o %n and %%n are the special characters which represent the parameter in a batch program, where n is a single digit, from 1 through 9. |
|
o The replaceable parameters are positional. The digit, n, of the special characters represents the position of the parameter we type with the batch command. |
|
For example: |
|
|
|
|
|
|
Batch Files in DOS |
|
How can we create a batch file? |
|
• EDIT command |
o Usage: EDIT filename.bat |
Where filename.bat is an optional parameter. We are recommended to give a name when we open the editor because it will simplify some steps in saving the file when it's done. |
|
o Run EDIT command at the command prompt, we will go to an editor mode, normally in blue screen. Make sure we have saved the file upon completion. |
|
o Remember to give an extension of .BAT in your batch file. |
|
|
• COPY command |
o Usage: COPY con filename.bat |
Where filename.bat is a required parameter if we want to save the file. We must give a name when we run COPY command. |
|
This method is useful when you are writing a short batch file. |
o "COPY con filename" |
1. Similar to backup a file using COPY command, "COPY con filename" means copying from "con" (the source) to a file called filename.bat (the target). |
|
2. CON is a reserved name for console. The keyboard is known to MS-DOS as console. |
|
3. By running the command, we instruct MS-DOS to copy what we type from the keyboard to a file filename.bat . When MS-DOS display a blinking cursor instead of a command prompt, it means it's ready for we to type in any text. |
|
4. Remember to use Ctrl-Z (i.e. hold down the ctrl key and hit Z) or press the function key F6 to end the file. MS-DOS will display ^Z on the screen. It is the end of file marker. |
|
5. And then, press Enter to save the file. MS-DOS will signal back "1 file(s) copied" and return to the command prompt. |
|
|
• DOSKEY command |
o Usage: DOSKEY /h > filename.bat |
|
o This method is useful for a "write-and-test" approach. While we are writing a batch file, we are working on the command prompt directly. By the way, we can know the result of every command immediately. |
|
o The DOSKEY program, once loaded into memory, keeps track of the commands we have typed through the keyboard. |
|
o DOSKEY /h -- displays all commands stored in memory. |
|
o By running the command, we instruct MS-DOS to redirect the history of commands to a batch file filename.bat . |
|
o Steps in using this method: |
1. Make sure DOSKEY program has been loaded into memory. |
|
2. Press Alt-F7 to clear the memory buffer before we "write-and-test" our file. |
|
3. Run the commands that we want to put in the batch file. |
|
4. Upon completion, run DOSKEY /h > filename.bat |
|
5. Run EDIT filename.bat to delete the last line "i.e. DOSKEY /h > filename.bat" and all other unnecessary lines, if necessary. |
|
|
|
|
|
|
|
|
|
No comments:
Post a Comment