![]() |
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-09-23 14:37 |
47,812 topics / 349,917 posts / today 0 new / 48,273 members |
| DOS批处理 & 脚本技术(批处理室) » Batch Files and Commands [repost] |
| Printable Version 2,720 / 14 |
| Floor1 tanglu_sd | Posted 2003-03-08 00:00 |
| 高级用户 Posts 271 Credits 948 From sd | |
|
Batch Files and Commands
In the DOS operating system you can often see some files with the extension BAT. What kind of files are these? What are they used for? How are they made up? That is what this section covers. Batch files A batch file is a text file in which a series of commonly used commands are written, like writing a program, with the extension “BAT”. Such a file is called a batch file. To run this batch file, you only need to type the filename of this batch file and press Enter, and the batch file will run all or part of the specified commands according to the commands in the file. Using batch files can simplify work and bring much convenience. Batch files can be created with software that has text-editing functions such as EDIT, WPS, CCED, etc.; they can even be created with the COPY CON command. A batch file can contain the following commands and statements: DOS internal and external commands, command files with the extensions COM, EXE, BAT, and batch commands. A file named AUTOEXEC。BAT is called an auto-executing batch file, and is a special batch file. When the computer starts, it will be searched for and executed automatically. Batch commands 1. ECHO command Function: allows or forbids the commands executed in the batch file to be displayed on the screen. Format: ECHO Parameter explanation: ON allows subsequent commands to be displayed on the screen when they are executed. OFF does not display subsequent commands on the screen when they are executed. @ECHO.. means that even this ECHO command itself is not displayed. ECHO+ means to display one blank line. MESSAGE is user-defined information. Whether ECHO is ON or OFF at the time, this information will be displayed on the screen when executed. 2. REM command Function: adds comments in a batch file. Format: REM Parameter explanation: string the content of the comment, up to 123 characters. 3. CALL command Function: calls and executes another batch file from one batch file, and after execution returns to the first batch file to continue running. Format: CALLfilename Parameter explanation: d:the drive where the called batch file is located. Path the path where the called batch file is located. Filename the filename of the called batch file. Batch file application techniques Although Windows 3.x and Windows95 are becoming more and more widely used, DOS is still loved by many users. As far as DOS batch files are concerned, many users have not brought their proper role into full play in actual use. This article discusses a few techniques for the application of DOS batch files, for readers' reference only. 1. Display selected DIR information In order to let users display special types of files in a directory, DOS's DIR command provides switches and wildcard functions. But the DIR command also has many unsatisfactory aspects. For example, if we want to display all files in a directory except those with the extension .TXT, the DIR command is powerless. The solution is to use the FIND command to achieve this function: DIR /A-D | FIND/V “TXT” The “/A-D” switch tells the DIR command not to display directories. The pipe command “|” specifies that the listed directory is displayed according to the execution result of the FIND command. The /V switch tells the FIND command to list all lines that do not include the string inside the double quotes. The string inside the double quotes should have a leading space (the space is to keep the FIND command from excluding files whose base filenames include the string TXT, such as NEWTXT.DOC, so the space cannot be omitted here). In fact, the FIND command can appear multiple times in one command line. For example, to display all files in a directory excluding those with the extensions .TXT and .DOC, the command is as follows: DIR /A-D | FIND/V“TXT”FIND/V“DOC” Below we give an example using the above technique. This example is used to display all files except those with specified extensions. Here we assume the filename is NEWDIR, and the command format is: NEWDIR Among them, files with extension1 and extension2 are the ones the user does not want displayed. @ECHO OFF IF“%1”==“”GOTO NOPARAM SET EXTENSION1=%1 SET EXTENSION2=%2 IF“%2”==“”SET EXTENSION2=. DIR /A-D | FIND /V /I“%EXTENSION1%” | FIND /V /I “%EXTENSION2%” |MORE GOTO END :NOPARAM ECHO No file extension specified. Syntax ECHO is: ECHO. ECHO NEWDIR ECHO. :END SET EXTENSION1= SET EXTENSION2= It is worth noting that a switch /I has been added after the FIND command. The function of this switch is to make the FIND command ignore the case of the extension typed on the command line. 2. Automate input When writing batch files, sometimes we need to specify the date in the file. Generally speaking, we add the DATE command to the batch file, so during execution the program will prompt the user to enter the date. Here we give a method of adding a carriage return after the DATE command: ECHO. | DATE ECHO. is used to send a carriage return to the screen, and the pipe symbol is used to redirect the carriage return to the DATE command. In order not to display the DATE command prompt, you can use the following command: ECHO. | DATE | FIND“Current” If you want to add a line containing the date to a text file, you can use the following command: ECHO. | DATE | FIND “Current” >THISDATE.TXT This method is also suitable for DOS's TIME command and FORMAT command. 3. Suppress displayed information In batch files, using “ECHO” together with “|” is very useful. Sometimes this combination can eliminate displayed information we do not wish to see. For example, to delete all files in a directory without wanting the system to display “Are you sure(Y/N)”, you can use the following command: ECHO Y | DEL *.* >NUL In actual use, we can use ECHO and the pipe symbol together appropriately so that the content displayed on the screen matches our requirements. 4. Set or reset the path As application systems increase, some applications require users to add their directories to the PATH statement, but the number of characters supported by PATH cannot exceed 127 characters, so in actual use this may sometimes become a limitation. Here we discuss a solution through an example. Suppose the path we have set is: PATH=C:\DOS;C:\UTILS;C:\BAT And now we need to run a word-processing program, and at the same time need to set the path to: PATH=C:\DOS;C:\UTILS;C:\BAT;C:\TEXTEDIT After running the above word-processing program, we also need to restore the path to its original state. Here we can accomplish this through the following batch program: @ECHO OFF SET OLDPATH=%PATH% PATH %PATH%;C:\TEXTEDIT CD C:\TEXTEDIT TEXTEDIT SET PATH=%OLDPATH% SET OLDPATH= CD C:\ The second line of the batch program saves the current path in the environment variable OLDPATH; the third line merges the paths; after the system finishes running on the sixth line, DOS hands control back to the batch program, which then resets the path to the original one. By this method we can solve the above problem. 5. Proper use of REM, ::, and GOTO The function of the REM statement in batch files is well known, but in actual use :: and GOTO can be used in place of REM statements, for example: ECHO The following lines are remarks that will not be display ::This line will not be display ::Neither will this line ECHO the batch file proceeds The same effect can also be achieved with a GOTO statement: ECHO The following lines are remarks that will not be display GOTO JUMP This line will not be display Neither will this line :JUMP ECHO The batch file proceeds Generally speaking, proper use of REM, ::, and GOTO will improve the efficiency of batch files. Usually, when comments are short, use :: or GOTO; when comments are long, use REM. This is because REM execution time is unrelated to the length of the comment, while :: and GOTO will take twice as long when the comment is long. 6. Convert user input to uppercase Anyone who writes batch files knows that the IF statement is case-sensitive. For example: IF“%1”==“ERASE” GOTO ERASE If the user types erase or Erase or any combination of upper and lower case for these five letters, DOS will consider that the IF statement condition has not been met, and therefore will not execute the GOTO statement. The solution is to use the PATH command to convert the lowercase letters entered by the user into uppercase. The program is as follows: SET OLDPATH=%PATH% SET PATH=%1 SET USERINPUT=%PATH% SET PATH=%OLDPATH% IF %USERINPUT%==ERASE GOTO ERASE SET OLDPATH= The first line stores the current path in the environment variable OLDPATH, the second line sets the PATH variable to the user input and converts the input content to uppercase, the third line sets an environment variable to PATH (that is, the content entered by the user), the fourth line restores the original PATH content, the fifth line uses the converted string in the IF statement, and the last line clears the OLDPATH environment variable. 7. FOR...IN...DO loop When more than one command needs to be executed at a time, the limitations of FOR...IN...DO become apparent. For example, in a batch file we cannot simply use the MORE command to display each file in a directory and then selectively delete them. The following example demonstrates a way to overcome this problem: @ECHO OFF IF“%1”==“:TASKS:” GOTO TASKS FOR %%A IN (%1) DO CALL %0 :TASKS: %%A GOTO END :TASKS CLS MORE %2 DEL /P %2 :END Here we name this batch program LOOPER. If we want to display all text files and then give a delete choice, we can type: LOOPER *.TXT Thus, on the first execution, the IF statement on the second line checks whether %1 contains :TASKS:. Since in this execution %1 does not contain :TASKS:, the program runs the FOR statement. In this example, the program first takes the first file with the extension .TXT and then executes the CALL command. Suppose the first text file is ABC.TXT, then DOS executes the following command: CALL LOOPER :TASKS: ABC.TXT That is, LOOPER runs a second copy of itself with two parameters (:TASKS: and ABC.TXT). When LOOPER runs the second time, it again checks whether %1 contains the value :TASKS:. This time the condition is satisfied, and LOOPER executes the program segment under :TASKS:. MORE displays the contents of ABC.TXT, and then DEL /P displays the choice of whether to delete the file. When the :TASKS: routine finishes execution, DOS closes the second LOOPER and returns to the FOR ...IN ...DO statement of the first LOOPER, thus proceeding to the next .TXT file. 8. Hide command output Many DOS commands display their results, but sometimes these results shown on the screen can leave the user dazzled. For example, COPY FILE1.TXT FILE2.TXT will display “1 file(s) copied” on the screen. If we do not want the system to display this message, we can use the following method: COPY FILE1.TXT FILE2.TXT>NUL This method is also suitable for the PAUSE command in batch files, and can suppress the PAUSE command's displayed message “Press any key to continue”. For example: @ECHO OFF ECHO There is no disk in drive A,Insert a ECHO disk and press Enter. PAUSE>NUL Subroutines in batch commands Most mature high-level programming languages allow you to establish commonly used routines as independent modules, that is, subroutines. It pre- serves the independence of the code, makes the program modular, and makes it easier to organize. At least it can be said that DOS batch commands do not fully support subrou- tines, but at any time you can use the GOTO command to call a subroutine. Usually, the label used by the GOTO command can also accept a label stored in an environment variable, for example: SET LABELNAME=START GOTO%LABELNAME% By replacing the label at the beginning of the subroutine and setting a GOTO %RETURN% statement at the end of the subroutine, you can establish subroutines in DOS batch commands. As long as you SET an environment variable RETURN, you can call a subrou- tine anywhere in the batch command. For example, you can assign a value to a label and call that subroutine on the next line, then the GOTO statement jumps to the start of that subroutine. The program structure is: REM This set the environment REM variable and calls the REM subroutine SET RETURN=HERE GOTO SUB HERE SUB REM Place subroutine statements REM below GOYO %RETURN% The statement GOTO SUB transfers to the subroutine SUB, where you can execute any statements. When execution reaches GOTO%RETURN%, the batch command returns control to the label HERE. Because the environment variable RETURN is assigned the value HERE again, if you use the same label (such as RETURN) each time you call SUB, then SUB can be called multiple times from multiple places. Batch file usage techniques Dong Zhanshan DOS batch files are very useful. We can write frequently used commands into batch files and have the computer automatically load the system to complete these repetitive tasks. Here are a few tips on using batch files: (1) Using DOS redirection DOS standard input and output are usually carried out on the standard devices keyboard and display. By using redirection, input and output can conveniently be redirected to disk files or other devices. For example, during execution of a batch command, in order to prevent the output information after the command or program is executed from cluttering the screen, DOS redirection can be used to redirect output to the NUL device (NUL does not point to any actual device): C:\>COPY A.TXT B.TXT > NUL。 After the command finishes, the message "1 file(s) copied" is not displayed. Some interactive programs require a lot of keyboard input when executing, but sometimes the input is fixed and unchanging. To speed up execution, an input file can be created in advance, with the contents of this file being the keyboard input items for the program, one item per line. Suppose there is a program ZB, and all its input items are included in the file IN.DAT. Execute C:\>ZB NUL and the program will run automatically. (2) Using DOS pipe functions DOS's pipe function makes the standard output of one program or command become the standard input of another program or command. For example, write DEBUG's input commands into the file AAA, then use the TYPE command to send the contents of AAA to DEBUG through the pipe function. During DEBUG execution it will no longer request command parameters from the console, thereby improving machine efficiency. The command is: C:\>TYPE AAA|DEBUG >BBB。 (3) Subroutines In one batch file, another sub-batch file can be called with the CALL command. When the sub-batch file finishes execution, it automatically returns to the parent batch file and continues downward. For example: A.BAT B.BAT,A calls B, and the contents of A.BAT are 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 call 31H or 4CH can be processed through the batch subcommands IF and ERRORLEVEL, so as to achieve the purpose of automatically executing a batch of commands. This makes it possible to implement all the menu prompt functions of high-level languages in a batch file, making the batch file more flexible and convenient. First use DEBUG to create a menu driver program MENU.COM, and correspondingly write a batch file LG.BAT. The specific content and method are shown in 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 '* 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 ASCD \TP5.00 TURBO CD \ GOTO START :BAS CD \TB TB CD \ GOTO START ROCD \TPROLOG PROLOG CD \ GOTO START :C CD \TURBOC TC CD \ GOTO START :EX @ECHO ON Run LG, and a menu will appear in the upper left corner of the screen, prompting the user to enter a selection. When the selected function finishes executing, it returns to the main menu and requests another selection, until function number "0" is selected, at which point the program ends and returns to DOS. (5) Using the command processor to complete a large amount of repetitive work DOS provides a method of calling a secondary command processor, which can implement a function equivalent to a subroutine, and 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. This will compile all FORTRAN source programs with the extension .FOR in the current directory into OBJ files. This method is fast and correct, requires little human-computer interaction, and reduces the programmer's 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 Using batch files correctly and skillfully can bring you twice the result with half the effort. By using the methods introduced in this article, you can easily write the main control module of an application system. It is just as handy as modules written in high-level languages, yet much simpler. The above programs were all tested on a PC/AT, with the operating system PC DOS3.30. |
|
| Floor2 MYS | Posted 2003-03-08 00:00 |
| 元老会员 Posts 1,637 Credits 5,170 From 广东佛山 | |
|
Very good, written in great detail. It can be called a quality post.
|
|
| Floor3 闪猛 | Posted 2003-03-08 00:00 |
| 中级用户 Posts 69 Credits 327 | |
|
Many thanks, this is exactly the kind of knowledge I need.
|
|
| Floor4 odusseus | Posted 2003-03-09 00:00 |
| 初级用户 Posts 11 Credits 127 | |
|
It's quite old. But it really is good.
|
|
| Floor5 chenall | Posted 2003-03-09 00:00 |
| 银牌会员 Posts 469 Credits 1,276 From 福建泉州 | |
|
Good stuff, bump bump bump bump.
|
|
| Floor6 liangjianan | Posted 2003-03-13 00:00 |
| 初级用户 Posts 1 Credits 109 | |
|
Thanks to tanglu_sd for the explanation, but I still have a few things to ask:
In Autoexec.bat on the Win98 boot disk there are these two lines: set LglDrv=27 * 26 Z 25 Y 24 X 23 W 22 V 21 U 20 T 19 S 18 R 17 Q 16 P 15 set LglDrv=%LglDrv% O 14 N 13 M 12 L 11 K 10 J 9 I 8 H 7 G 6 F 5 E 4 D 3 C I don't know how to interpret them. Also: how can the CD-ROM drive letter be automatically placed immediately after the hard disk drive letters, like on the 98 boot disk? |
|
| Floor7 lkkl | Posted 2003-03-20 00:00 |
| 初级用户 Posts 3 Credits 113 | |
|
Quality post!!!
|
|
| Floor8 liuguang2008 | Posted 2003-03-21 00:00 |
| 初级用户 Posts 13 Credits 157 | |
|
Very worthwhile to study, I should learn from you properly.
|
|
| Floor9 HR130 | Posted 2003-03-31 00:00 |
| 初级用户 Posts 16 Credits 172 | |
| Floor10 bgn | Posted 2003-04-02 00:00 |
| 中级用户 Posts 78 Credits 349 | |
|
Very good, very good, I'm very happy. Thankyou!
|
|
| Floor11 zjktxwd | Posted 2003-04-09 00:00 |
| 初级用户 Posts 20 Credits 193 | |
|
Excellent, thanks!
|
|
| Floor12 luxia | Posted 2003-04-11 00:00 |
| 初级用户 Posts 4 Credits 120 | |
|
Good
|
|
| Floor13 13670767243 | Posted 2005-01-08 00:00 |
| 初级用户 Posts 19 Credits 102 | |
|
Quality post!!!
Excellent, thanks! |
|
| Floor14 13670767243 | Posted 2005-01-09 00:00 |
| 初级用户 Posts 19 Credits 102 | |
|
set LglDrv=27 * 26 Z 25 Y 24 X 23 W 22 V 21 U 20 T 19 S 18 R 17 Q 16 P 15
set LglDrv=%LglDrv% O 14 N 13 M 12 L 11 K 10 J 9 I 8 H 7 G 6 F 5 E 4 D 3 C What the above means is assigning all the characters after the equals sign "=" to the variable %LglDrv%. |
|
| Floor15 xlxlds | Posted 2005-01-09 00:00 |
| 初级用户 Posts 14 Credits 141 | |
|
Not bad, I really do need this kind of thing. ! Can anyone else understand the code after the = below? If you know, could you explain how to read it? set LglDrv=27 * 26 Z 25 Y 24 X 23 W 22 V 21 U 20 T 19 S 18 R 17 Q 16 P 15
set LglDrv=%LglDrv% O 14 N 13 M 12 L 11 K 10 J 9 I 8 H 7 G 6 F 5 E 4 D 3 C |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |