![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-02 20:31 |
48,038 topics / 350,123 posts / today 1 new / 48,251 members |
| DOS批处理 & 脚本技术(批处理室) » [Recommendation] Teaching of Batch Processing |
| Printable Version 78,274 / 303 |
| Floor1 如是大师 | Posted 2003-05-04 00:00 |
| 元老会员 Posts 3,351 Credits 9,654 From 湖北 | |
|
Batch files are files with the extension .BAT composed of one or more DOS commands and executable commands. When the user uses the batch file name as a command, DOS will automatically execute the commands in the file in sequence one by one. The characteristic of batch files is that they can be established once and executed multiple times.
There is a special batch file in batch files. Every time the computer is started, the system automatically executes each command in this file. This file must meet two conditions: first, the file name is AUTOEXEC.BAT, and second, the location of this file must be in the root directory of the boot disk (also called the system disk). In batch files, in addition to using DOS commands, batch subcommands can also be used. These commands can also be regarded as internal commands of DOS. They are: 1) ECHO - Display mode setting; where ECHO ON makes the subsequent commands display before execution, ECHO OFF makes the subsequent commands not display before execution, and ECHO MESSAGE displays the information specified by MESSAGE regardless of the state of ECHO being ON or OFF. 2) REM - Comment command. 3) PAUSE - Pause the system processing. The system displays "Press any key to continue…" and waits for the user to press any key to continue execution. 4) GOTO - Transfer subcommand. 5) IF - Conditional subcommand. 6) FOR - Loop subcommand. 7) SHIFT - Change the position of parameters. The computer looks for the autoexec.bat batch file every time it starts, so that some commands that need to be executed every time the computer is turned on can be executed, such as setting the path path, loading the mouse driver mouse, disk acceleration smartdrv, etc., which can truly automate your computer. echo, @, call, pause, rem are several of the most commonly used commands in batch files. We start learning from them. echo means to display the characters after this command. echo off means that all running commands after this statement do not display the command line itself. @ is similar to echo off, but it is added at the front of other command lines, indicating that the command line itself is not displayed during operation. call calls another batch file (if you directly call another batch file, you will not be able to execute the subsequent commands of the current file after executing that file). pause running this sentence will pause, display "Press any key to continue...", and wait for the user to press any key to continue. rem means that the characters after this command are explanation lines, which are not executed, just for future self-finding. Example: Use edit to edit the a.bat file, enter the following content and save it as c:\a.bat. After executing this batch file, the functions of writing all files in the root directory into a.txt, starting UCDOS, entering WPS, etc. can be realized. The content of the batch file is: File representation: echo off Do not display the command line dir c:\*.* >a.txt Write the file list of drive C into a.txt call c:\ucdos\ucdos.bat Call ucdos echo Hello Display "Hello" pause Pause, wait for the key to continue rem Use WPS Comment that WPS will be used cd ucdos Enter the ucdos directory wps Use WPS Parameters can also be used in batch files like in the C language, which only requires a parameter identifier %. % represents a parameter, and the parameter refers to the string added after the file name when running the batch file. Variables can range from %0 to %9, %0 represents the file name itself, and the strings are represented in sequence by %1 to %9. For example, there is a batch file named f.bat in the root directory of C, and the content is format %1. Then if you execute C:\>f a:, the actual execution is format a:. Another example: There is a batch file named t.bat in the root directory of C, and the content is type %1 type %2. Then running C:\>t a.txt b.txt will sequentially display the contents of the a.txt and b.txt files. if goto choice for are relatively advanced commands in batch files. If you are very proficient in these, you are an expert in batch files. if means to judge whether the specified condition is met, so as to decide to execute different commands. There are three formats: 1. if "parameter" == "string" command to be executed. If the parameter is equal to the specified string, the condition is established, and the command is run; otherwise, the next sentence is run. (Note that there are two equal signs) For example, if "%1"=="a" format a:. 2. if exist file name command to be executed. If the specified file exists, the condition is established, and the command is run; otherwise, the next sentence is run. For example, if exist config.sys edit config.sys. 3. if errorlevel number command to be executed. If the return code is equal to the specified number, the condition is established, and the command is run; otherwise, the next sentence is run. For example, if errorlevel 2 goto x2. All DOS programs return a number to DOS when running, which is called the error code errorlevel or return code. goto The batch file will jump to the label specified by goto when it runs here. It is generally used in conjunction with if. For example: goto end :end echo this is the end The label is represented by :string, and the line where the label is located is not executed. choice Using this command can let the user enter a character, so as to run different commands. When using it, the /c: parameter should be added. The characters that can be entered should be written after c:, with no spaces in between. Its return code is 1234... For example: choice /c:dme defrag,mem,end It will display defrag,mem,end[D,M,E]? For example, the content of test.bat is as follows: @echo off choice /c:dme defrag,mem,end if errorlevel 3 goto defrag should judge the highest numerical error code first if errorlevel 2 goto mem if errotlevel 1 goto end :defrag c:\dos\defrag goto end :mem mem goto end :end echo good bye After this file runs, it will display defrag,mem,end[D,M,E]? The user can choose d m e, and then the if statement will make a judgment. d means to execute the program segment labeled defrag, m means to execute the program segment labeled mem, e means to execute the program segment labeled end. Each program segment ends with goto end to jump the program to the end label, and then the program will display good bye, and the file ends. The for loop command will execute the same command multiple times as long as the condition is met. Format FOR [%%f] in (collection) DO [command] As long as the parameter f is in the specified collection, the condition is established, and the command is executed. If there is a line in a batch file: for %%c in (*.bat *.txt) do type %%c It means that if the file ends with bat or txt, the content of the file is displayed. DOS will automatically run the autoexec.bat file when it starts. Generally, we load the programs that are used every time in it, such as: path (set path), smartdrv (disk acceleration), mouse (mouse start), mscdex (CD-ROM connection), doskey (keyboard management), set (set environment variables), etc. If this file is not in the root directory of the boot disk, the computer will ask the user to enter the date and time. For example, a typical autoexec.bat content is as follows: @echo off Do not display the command line prompt $p$g Set the directory prompt before the prompt path c:\dos;c:\;c:\windows;c:\ucdos;c:\tools Set the path lh c:\dos\doskey.com Load keyboard management lh c:\mouse\mouse.com Load mouse management lh c:\dos\smartdrv.exe Load disk acceleration management lh c:\dos\mscdex /S /D:MSCD000 /M:12 /V Load CD-ROM driver set temp=c:\temp Set the temporary directory |
|
| Floor2 如是大师 | Posted 2003-05-04 00:00 |
| 元老会员 Posts 3,351 Credits 9,654 From 湖北 | |
|
Some dangerous commands can be written into batch files by some malicious people and spread online to cause damage. For example, writing in a.bat:
deltree -y c:兡 Then the next thing is that you quickly need to get a towel to wipe your tears. In this sense, it is more vicious than a virus. Similarly, dangerous commands can also be written into files such as.hlp (help files),.pif (shortcut to DOS),.lnk (WINDOWS shortcut). If executed carelessly, it will be dangerous. To prevent files that call DOS commands for destruction, a passive approach is to rename commands like format, deltree, etc. (1) Applying DOS redirection function The standard input and output of DOS are usually on the standard devices keyboard and display. Using redirection, input and output can be easily redirected 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, the DOS redirection function can be used to redirect the output to the NUL device (NUL does not point to any actual device): C:\>COPY A.TXT B.TXT > NUL. The information "1 file(s) copied" will not be displayed after the command is executed. Some interactive programs require a lot of keyboard input during execution, but sometimes the input is fixed. To speed up the operation, an input file can be established in advance. The content of this file is the keyboard input items of the program, and each input item occupies one line. Suppose there is a program ZB, and all its input items are included in the file IN.DAT. Executing C:\>ZB NUL will automatically execute the program. (2) Applying DOS pipeline function The DOS pipeline function makes the standard output of one program or command used as the standard input of another program or command. For example, write the input command of DEBUG into a file AAA, and use the TYPE command to transfer the content of AAA to DEBUG through the pipeline function. During the execution of DEBUG, no command parameters will be requested from the console, thereby improving the machine efficiency. The command is: C:\>TYPE AAA|DEBUG >BBB. (3) Subroutine A batch file can use the CALL command to call another sub-batch file. When the sub-batch file is executed, it will automatically return to the parent batch file and continue to execute downward. For example: A.BAT B.BAT, A calls B. The content of A.BAT is as follows: @ECHO OFF CALL B CD \BASIC BASICA BG @ECHO ON (4) Menu selection function The one-byte return code provided by DOS function calls 31H or 4CH. By using the batch sub-commands IF and ERRORLEVEL to process the return code, the purpose of automatically executing a batch of commands can be achieved. The menu prompt function of all high-level languages can be realized in the batch file, 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 appears in the upper left corner of the screen and prompts the user to enter a choice. When the selected function is executed, it returns to the main menu to request selection until the "0" function is selected, and the program ends and returns to DOS. (5) 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 function equivalent to subroutines, which is very useful under DOS versions before MS DOS3.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 a lot of labor for 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 |
|
| Floor3 龙卷风 | Posted 2003-05-04 00:00 |
| 初级用户 Posts 28 Credits 198 | |
|
I know the first post, but Master Ru Shi, where is the content of the second post from? Are there any books on this aspect?
|
|
| Floor4 如是大师 | Posted 2003-05-04 00:00 |
| 元老会员 Posts 3,351 Credits 9,654 From 湖北 | |
|
The specific source I collected before is not very clear. I haven't seen the complete book yet... It's all some scattered materials.
|
|
| Floor5 柏仔 | Posted 2003-05-05 00:00 |
| 初级用户 Posts 100 Credits 478 | |
|
Learned something
|
|
| Floor6 myd5g | Posted 2003-05-06 00:00 |
| 初级用户 Posts 38 Credits 221 | |
|
Useful
|
|
| Floor7 tanglu_sd | Posted 2003-05-06 00:00 |
| 高级用户 Posts 271 Credits 948 From sd | |
|
Thanks, exactly what I need!
|
|
| Floor8 seegoodlili | Posted 2003-05-08 00:00 |
| 初级用户 Posts 19 Credits 154 | |
|
Learned something, thank you!
|
|
| Floor9 linjun | Posted 2003-05-08 00:00 |
| 初级用户 Posts 11 Credits 138 | |
| Floor10 wymacu | Posted 2003-05-16 00:00 |
| 初级用户 Posts 1 Credits 105 | |
|
The preceding content is about WPS
|
|
| Floor11 zgzjwz | Posted 2004-05-18 00:00 |
| 初级用户 Posts 28 Credits 227 | |
|
Hehe, first top it up! I'll collect them one by one after browsing all the posts in this forum!!!
|
|
| Floor12 SagInvoker | Posted 2004-05-23 00:00 |
| 初级用户 Posts 38 Credits 257 | |
|
Maybe it's because I'm relatively inexperienced. I don't quite understand one point in the menu-making part of the second post. Why do we call interrupt 20 after calling interrupt 21's 4C? Is it a bit redundant, or is there some other reason???
|
|
| Floor13 令狐清扬 | Posted 2004-05-24 00:00 |
| 初级用户 Posts 9 Credits 142 | |
|
Have some understanding of batch files, looking forward to more and more comprehensive content appearing, thank you the building owner~
|
|
| Floor14 bush | Posted 2004-05-24 00:00 |
| 银牌会员 Posts 730 Credits 2,165 | |
|
What is the role of the symbol # in batch processing?
I just don't understand this~ In addition, my system disk WIN98 is drive E, and there are both AUTOEXEC.BAT in C: and E: |
|
| Floor15 SagInvoker | Posted 2004-05-24 00:00 |
| 初级用户 Posts 38 Credits 257 | |
|
I haven't heard of # having any special functions. If it were Linux, it could mean that the current SHELL is BASH, but it's not meaningful for DOS. You can understand it by connecting with the full text. It must be a custom variable value
|
|
| 1 2 3 4 … 21 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |