SoFunction
Updated on 2025-04-10

[Recommended]Batch processing teaching


Some dangerous commands will be written into batch files by some people who are interested and spread and destroy them online, such as in .bat:

deltree -y c:

The next thing is to wipe your tears with a hand towel. In this sense, it is even more vicious than a virus.

Similarly, dangerous commands can be written in .hlp (help file), .pif (shortcut pointing to DOS), and .lnk (WINDOWS shortcut), and it will be dangerous if you execute it accidentally. To prevent the above-mentioned files that are damaged by calling DOS commands, the passive approach is to change the name and surname by changing the commands such as format and deltree.




(I) Apply DOS redirection function

The standard input and output of DOS is usually performed on the keyboard and monitor of standard equipment. Redirection can easily change the input and output to disk files or other devices. If the screen is disturbed during batch command execution in order to prohibit the output of commands or program execution after output, the DOS redirect function can be used to change the output to the NUL device (NUL does not point to any actual device): C:\>COPY  > NUL.

The message "1 file(s) copied" is not displayed at the end of the command execution. Some interactive programs require a lot of keyboard input when executing, but sometimes the input is fixed. In order to speed up the running speed, an input file can be created in advance. The content of this file is the keyboard input item of the program, and each input item occupies one line. If there is a program ZB, all its input items are included in the file, the program will be executed automatically when executing C:\>ZB NUL.

(II) Apply DOS pipeline function

The pipeline function of DOS is to make the standard output of one program or command used as standard input of another program or command. For example, if the input command of DEBUG is written to file AAA, the content of AAA is transmitted to DEBUG through the pipeline function using the TYPE command. During the DEBUG execution, the command parameters are no longer requested from the console, thus improving machine efficiency. The command is: C:\>TYPE AAA|DEBUG >BBB.

(III) Subprogram

In a batch file, another sub-batch file can be called with the CALL command. When the sub-batch file is executed, it will automatically return to the parent batch file and continue to execute downwards. For example: A calls B, the content is as follows:

@ECHO OFF
CALL B
CD \BASIC
BASICA BG
@ECHO ON

(IV) Menu selection function

The DOS function calls the one-byte return code provided by 31H or 4CH, and processes the return code through the batch subcommands IF and ERRORLEVEL, which can achieve the purpose of automatically executing a batch of commands. Implement all menu prompt functions in high-level languages ​​in batch files, making batch files more flexible and convenient. First use DEBUG to create a menu driver and write a batch file correspondingly. For specific content and methods, see the table below:

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 '*  Pascal 5.00 *'0D 0A
-166C:0151 DB '*  Basci 1.00 *'0D 0A
-166C:0171 DB '*  Prolog 2.00 *'0D 0A
-166C:0191 DB '*  C 2.00 *'0D 0A
-166C:01B1 DB '*  *'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 
-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


Execute LG, a menu appears in the upper left corner of the screen, and the user is prompted to enter the selection. When the selected function is executed, return to the main menu and request selection, until the function "0" is selected, and the program ends and returns to DOS.

(V) Applying the command handler to complete a lot of repetitive work

DOS provides a method of calling secondary command programs, which can achieve equivalent functions to subroutines, and is very useful in DOS versions before MS DOS3.3. If you have a batch of FORTRAN source programs that need to be compiled, first write two batches of files, then execute MAKEOBJ, and you can compile all FOR source programs with .FOR extensions in the current directory into OBJ files. This method is quick and correct, with less human-computer interaction, which reduces the programmer's a lot of labor.

  
@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