『楼 主』:
转贴:DOS批处理文件使用技巧
使用 LLM 解释/回答一下
批处理文件使用技巧
董占山
DOS的批处理文件十分有用,我们可以把经常使用的命令编写为批处理文件,由计算机来实现对系统的自动加载, 完成这些重复工作。今介绍几个批处理文件的使用技巧:
(一)应用DOS重定向功能
DOS的标准输入输出通常是在标准设备键盘和显示器上进行的, 利用重定向,可以方便地将输入输出改向磁盘文件或其它设备。如在批处理命令执行期间为了禁止命令或程序执行后输出信息而扰乱屏幕, 可用DOS重定向功能把输出改向NUL设备(NUL不指向任何实际设备): C:\>COPY A.TXT B.TXT > NUL。
命令执行结束不显示"1 file(s) copied"的信息。有的交互程序在执行时要求很多键盘输入, 但有时输入是固定不变的, 为加快运行速度, 可预先建立一个输入文件,此文件的内容为程序的键盘输入项, 每个输入项占一行。假如有一个程序ZB, 其输入项全部包括在文件IN.DAT中, 执行 C:\>ZB NUL 程序就自动执行。
(二)应用DOS管道功能
DOS的管道功能是使一个程序或命令的标准输出用做另一个程序或命令的标准输入。如把DEBUG的输入命令写入文件AAA, 用TYPE命令通过管道功能将AAA的内容传输给DEBUG, 在DEBUG执行期间不再从控制台索取命令参数, 从而提高了机器效率。命令为: C:\>TYPE AAA|DEBUG >BBB。
(三)子程序
在一个批处理文件可用CALL命令调用另一个子批处理文件, 当子批文件执行结束后,自动返回父批文件, 继续向下执行。如: A.BAT B.BAT,A调用B,A.BAT内容如下:
@ECHO OFF
CALL B
CD \BASIC
BASICA BG
@ECHO ON
(四)菜单选择功能
DOS功能调用31H或4CH所提供的一字节的返回码, 通过批处理子命令IF和ERRORLEVEL对返回码进行处理, 可达到自动执行一批命令的目的。在批处理文件中实现高级语言所有的菜单提示功能, 使批处理文件变得更灵活方便。先用DEBUG建立一个菜单驱动程序MENU.COM,对应地编写一个批处理文件LG.BAT。具体内容和方法见下表:
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
<img src="images/smilies/face-raspberry.png" align="absmiddle" border="0">AS
CD \TP5.00
TURBO
CD \
GOTO START
:BAS
CD \TB
TB
CD \
GOTO START
<img src="images/smilies/face-raspberry.png" align="absmiddle" border="0">RO
CD \TPROLOG
PROLOG
CD \
GOTO START
:C
CD \TURBOC
TC
CD \
GOTO START
:EX
@ECHO ON
执行LG, 屏幕左上角出现一个菜单, 并提示用户输入选择, 当选择的功能执行结束,重新返回主菜单请求选择, 直到选择"0"号功能, 程序结束返回DOS。
(五)应用命令处理程序完成大量重复工作
DOS提供调用次级命令程序的方法, 可实现与子程序等效的功能, 在MS DOS3.3以前的DOS版本下非常有用。如你有一批FORTRAN源程序需要编译, 首先编写两个批文件MAKEOBJ.BAT、C.BAT, 然后执行MAKEOBJ, 即可把当前目录下的所有扩展名为.FOR的FORTRAN源程序编译成OBJ文件。这种方法迅速正确, 人机交互少, 减轻了程序员的
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
正确熟练地应用批处理文件, 可以给你的工作带来事半功倍的效果。利用本文介绍的方法你可以方便地编写出一个应用系统的主控模块, 与高级编写的模块一样好用, 且更为简便。以上的程序均在PC/AT上通过, 操作系统为PC DOS3.30。
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
:PAS
CD \TP5.00
TURBO
CD \
GOTO START
:BAS
CD \TB
TB
CD \
GOTO START
:PRO
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.
|