Batch File Usage Tips
Dong Zhanshan
DOS batch files are very useful. We can write frequently used commands into batch files, and let the computer automatically load the system to complete these repetitive tasks. Now, let's introduce several tips for using batch files:
### (I) Applying DOS Redirection Function
The standard input and output of DOS are usually carried out on the standard devices keyboard and display. Using redirection, we can easily redirect input and output to disk files or other devices. For example, during the execution of a batch command, to prohibit the output information from disturbing the screen after the command or program is executed, we can use the DOS redirection function to redirect the output to the NUL device (NUL does not point to any actual device): C:\>COPY A.TXT B.TXT > NUL.
After the command is executed, the information "1 file(s) copied" will not be displayed. Some interactive programs require a lot of keyboard input when executed, but sometimes the input is fixed. To speed up the running speed, we can pre-establish an input file, and the content of this file is the keyboard input items of the program, with each input item occupying one line. Suppose there is a program ZB, and all its input items are included in the file IN.DAT. Executing C:\>ZB < IN.DAT will make the program execute automatically.
### (II) Applying DOS Pipeline Function
The pipeline function of DOS makes the standard output of one program or command be used as the standard input of another program or command. For example, write the input command of DEBUG into the file AAA, and use the TYPE command to transmit the content of AAA to DEBUG through the pipeline function. During the execution of DEBUG, it will no longer obtain command parameters from the console, thereby improving the machine efficiency. The command is: C:\>TYPE AAA|DEBUG >BBB.
### (III) Subroutines
In a batch file, the CALL command can be used to call another sub-batch file. When the sub-batch file finishes execution, it will automatically return to the parent batch file and continue to execute downward. For example: A.BAT calls B.BAT. The content of A.BAT is as follows:
@ECHO OFF
CALL B
CD \BASIC
BASICA BG
@ECHO ON
### (IV) Menu Selection Function
The one-byte return code provided by the DOS function call 31H or 4CH is processed by using the batch sub-commands IF and ERRORLEVEL to handle the return code, which can achieve the purpose of automatically executing a batch of commands. In the batch file, the menu prompt function of high-level languages can be realized, making the batch file more flexible and convenient. First, use DEBUG to create a menu-driven program MENU.COM, and correspondingly write a batch file LG.BAT. The specific content and method are shown in the following table:
DEBUG
-A
-166C:0100 MOV DX,111
-166C:0103 MOV AH,09
-166C:0105 INT 21
-166C:0107 MOV AH,01
-166C:0109 INT 21
-166C:010B MOV AH,4C
-166C:010D INT 21
-166C:010F INT 20
-166C:0111 DB '******************************'0D 0A
-166C:0131 DB '* 1.Turbo Pascal 5.00 *'0D 0A
-166C:0151 DB '* 2.Turbo Basci 1.00 *'0D 0A
-166C:0171 DB '* 3.Turbo Prolog 2.00 *'0D 0A
-166C:0191 DB '* 4.Turbo C 2.00 *'0D 0A
-166C:01B1 DB '* 0.Exit *'0D 0A
-166C:01B1 DB '******************************'0D 0A
-166C:01F1 DB 'Your choice(0..4) : '24 0D 0A 1A
-166C:0209
-R CX
CX 0000
:108
-N MENU.COM
-W
Writing 0108 bytes
-Q
@ECHO OFF:
START
CLS
MENU
IF ERRORLEVEL 52 GOTO C
IF ERRORLEVEL 51 GOTO PRO
IF ERRORLEVEL 50 GOTO BAS
IF ERRORLEVEL 49 GOTO PAS
IF ERRORLEVEL 48 GOTO EX
CLS
GOTO START
AS
CD \TP5.00
TURBO
CD \
GOTO START
:BAS
CD \TB
TB
CD \
GOTO START
RO
CD \TPROLOG
PROLOG
CD \
GOTO START
:C
CD \TURBOC
TC
CD \
GOTO START
:EX
@ECHO ON
When executing LG, a menu will appear in the upper left corner of the screen, and it will prompt the user to enter a choice. When the selected function finishes execution, it will return to the main menu to request a choice again until the function numbered "0" is selected, and the program will end and return to DOS.
### (V) Using the Command Processor to Complete a Large Number of Repetitive Tasks
DOS provides a method to call secondary command programs, which can realize the same function as subroutines and is very useful under DOS versions before MS DOS 3.3. For example, if you have a batch of FORTRAN source programs that need to be compiled, first write two batch files MAKEOBJ.BAT and C.BAT, and then execute MAKEOBJ, which can compile all FORTRAN source programs with the extension.FOR in the current directory into OBJ files. This method is fast and correct, with less human-computer interaction, and reduces the burden on programmers.
MAKEOBJ.BAT C.BAT
@ECHO OFF
ECHO COMPILE FORTRAN PROGRAMS.
FOR %%A IN (*.FOR) DO COMMAND /C C %%A
ECHO FINISH!
@ECHO ON @ECHO OFF
ECHO ------ COMPILE %1 ------
FOR1 %1; >NUL
FOR2 >NUL
@ECHO ON
Correctly and skillfully applying batch files can bring you twice the result with half the effort. Using the methods introduced in this article, you can easily write a main control module of an application system, which is as easy to use as a module written in a high-level language and is more convenient. The above programs all passed on the PC/AT, with the operating system being PC DOS 3.30.
Dong Zhanshan
DOS batch files are very useful. We can write frequently used commands into batch files, and let the computer automatically load the system to complete these repetitive tasks. Now, let's introduce several tips for using batch files:
### (I) Applying DOS Redirection Function
The standard input and output of DOS are usually carried out on the standard devices keyboard and display. Using redirection, we can easily redirect input and output to disk files or other devices. For example, during the execution of a batch command, to prohibit the output information from disturbing the screen after the command or program is executed, we can use the DOS redirection function to redirect the output to the NUL device (NUL does not point to any actual device): C:\>COPY A.TXT B.TXT > NUL.
After the command is executed, the information "1 file(s) copied" will not be displayed. Some interactive programs require a lot of keyboard input when executed, but sometimes the input is fixed. To speed up the running speed, we can pre-establish an input file, and the content of this file is the keyboard input items of the program, with each input item occupying one line. Suppose there is a program ZB, and all its input items are included in the file IN.DAT. Executing C:\>ZB < IN.DAT will make the program execute automatically.
### (II) Applying DOS Pipeline Function
The pipeline function of DOS makes the standard output of one program or command be used as the standard input of another program or command. For example, write the input command of DEBUG into the file AAA, and use the TYPE command to transmit the content of AAA to DEBUG through the pipeline function. During the execution of DEBUG, it will no longer obtain command parameters from the console, thereby improving the machine efficiency. The command is: C:\>TYPE AAA|DEBUG >BBB.
### (III) Subroutines
In a batch file, the CALL command can be used to call another sub-batch file. When the sub-batch file finishes execution, it will automatically return to the parent batch file and continue to execute downward. For example: A.BAT calls B.BAT. The content of A.BAT is as follows:
@ECHO OFF
CALL B
CD \BASIC
BASICA BG
@ECHO ON
### (IV) Menu Selection Function
The one-byte return code provided by the DOS function call 31H or 4CH is processed by using the batch sub-commands IF and ERRORLEVEL to handle the return code, which can achieve the purpose of automatically executing a batch of commands. In the batch file, the menu prompt function of high-level languages can be realized, making the batch file more flexible and convenient. First, use DEBUG to create a menu-driven program MENU.COM, and correspondingly write a batch file LG.BAT. The specific content and method are shown in the following table:
DEBUG
-A
-166C:0100 MOV DX,111
-166C:0103 MOV AH,09
-166C:0105 INT 21
-166C:0107 MOV AH,01
-166C:0109 INT 21
-166C:010B MOV AH,4C
-166C:010D INT 21
-166C:010F INT 20
-166C:0111 DB '******************************'0D 0A
-166C:0131 DB '* 1.Turbo Pascal 5.00 *'0D 0A
-166C:0151 DB '* 2.Turbo Basci 1.00 *'0D 0A
-166C:0171 DB '* 3.Turbo Prolog 2.00 *'0D 0A
-166C:0191 DB '* 4.Turbo C 2.00 *'0D 0A
-166C:01B1 DB '* 0.Exit *'0D 0A
-166C:01B1 DB '******************************'0D 0A
-166C:01F1 DB 'Your choice(0..4) : '24 0D 0A 1A
-166C:0209
-R CX
CX 0000
:108
-N MENU.COM
-W
Writing 0108 bytes
-Q
@ECHO OFF:
START
CLS
MENU
IF ERRORLEVEL 52 GOTO C
IF ERRORLEVEL 51 GOTO PRO
IF ERRORLEVEL 50 GOTO BAS
IF ERRORLEVEL 49 GOTO PAS
IF ERRORLEVEL 48 GOTO EX
CLS
GOTO START
AS CD \TP5.00
TURBO
CD \
GOTO START
:BAS
CD \TB
TB
CD \
GOTO START
RO CD \TPROLOG
PROLOG
CD \
GOTO START
:C
CD \TURBOC
TC
CD \
GOTO START
:EX
@ECHO ON
When executing LG, a menu will appear in the upper left corner of the screen, and it will prompt the user to enter a choice. When the selected function finishes execution, it will return to the main menu to request a choice again until the function numbered "0" is selected, and the program will end and return to DOS.
### (V) Using the Command Processor to Complete a Large Number of Repetitive Tasks
DOS provides a method to call secondary command programs, which can realize the same function as subroutines and is very useful under DOS versions before MS DOS 3.3. For example, if you have a batch of FORTRAN source programs that need to be compiled, first write two batch files MAKEOBJ.BAT and C.BAT, and then execute MAKEOBJ, which can compile all FORTRAN source programs with the extension.FOR in the current directory into OBJ files. This method is fast and correct, with less human-computer interaction, and reduces the burden on programmers.
MAKEOBJ.BAT C.BAT
@ECHO OFF
ECHO COMPILE FORTRAN PROGRAMS.
FOR %%A IN (*.FOR) DO COMMAND /C C %%A
ECHO FINISH!
@ECHO ON @ECHO OFF
ECHO ------ COMPILE %1 ------
FOR1 %1; >NUL
FOR2 >NUL
@ECHO ON
Correctly and skillfully applying batch files can bring you twice the result with half the effort. Using the methods introduced in this article, you can easily write a main control module of an application system, which is as easy to use as a module written in a high-level language and is more convenient. The above programs all passed on the PC/AT, with the operating system being PC DOS 3.30.
我的网志
http://hzmys.blog.163.com/
我的网盘
firststep.qjwm.com
fsmys.ys168.com
ssmys.ys168.com
www.brsbox.com/fsmys
www.brsbox.com/ssmys
www.brsbox.com/ccdos
http://hzmys.blog.163.com/
我的网盘
firststep.qjwm.com
fsmys.ys168.com
ssmys.ys168.com
www.brsbox.com/fsmys
www.brsbox.com/ssmys
www.brsbox.com/ccdos



