China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 11:03
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Repost: Tips for Using DOS Batch Files View 3,719 Replies 4
Original Poster Posted 2002-10-25 00:00 ·  中国 广东 佛山 禅城区 电信
元老会员
★★★★
Credits 5,170
Posts 1,637
Joined 2002-10-16 00:00
23-year member
UID 8
Gender Male
From 广东佛山
Status Offline
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.
我的网志
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
Floor 2 Posted 2003-09-16 00:00 ·  中国 广东 汕头 潮阳区 电信
中级用户
Credits 204
Posts 23
Joined 2003-09-16 00:00
22-year member
UID 9875
Gender Male
Status Offline
I really want to learn, but after looking at it for a long time, I still don't understand what it's saying. Can you simplify it?
Floor 3 Posted 2003-09-23 00:00 ·  中国 北京 科技网
银牌会员
★★★
颓废青年
Credits 2,265
Posts 721
Joined 2003-05-12 00:00
23-year member
UID 2032
Gender Male
Status Offline
Bump!
Floor 4 Posted 2003-09-24 00:00 ·  中国 上海 东方有线
中级用户
★★
Credits 333
Posts 62
Joined 2003-09-16 00:00
22-year member
UID 9869
Gender Male
Status Offline
My head’s spinning.
Floor 5 Posted 2003-09-24 00:00 ·  中国 辽宁 大连 庄河市 联通
初级用户
Credits 112
Posts 3
Joined 2003-09-19 00:00
22-year member
UID 10034
Gender Male
Status Offline
Not bad, make up some lessons.
Forum Jump: