![]() |
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-13 18:40 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS学习入门 & 精彩文章 (教学室) » Repost: Practical Tips for Using Batch Files |
| Printable Version 1,556 / 4 |
| Floor1 Lydong | Posted 2003-02-24 00:00 |
| 元老会员 Posts 407 Credits 1,468 From 广州 | |
|
Practical Tips for Using Batch Files
Although Windows 3.x and Windows95 are being used more and more widely, DOS is still loved by many users. As for DOS batch files, many users have not fully brought their proper role into play in actual use. This article discusses a few tips on the use 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 switch and wildcard functions. But the DIR command also has many unsatisfactory aspects. For example, if we want to display files in a directory other than 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 be displayed according to the execution result of the FIND command, and the /V switch tells the FIND command to list all lines that do not include the string inside the quotation marks. The string inside the quotation marks must be preceded by a space (the space prevents the FIND command from excluding files whose primary filename contains the string TXT, such as NEWTXT.DOC, so the space cannot be omitted here). In fact, the FIND command can appear many times in one command line. For example, to display all files in a directory except 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 the filename is assumed to be NEWDIR, and the command format is: NEWDIR Among them, files with extension1 and extension2 are the ones the user does not want to display. @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 entered on the command line. 2.Automating input When we write batch files, sometimes we need to specify the date in the file. Generally speaking, we add the DATE command to the batch file, and 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's prompt, you can use the following command: ECHO. | DATE | FIND“Current” If you want to add a line with 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.Suppressing displayed messages In batch files, the combination of “ECHO” and “|” is very useful. Sometimes this combination can eliminate displayed information we do not want to see. For example, to delete all files in a directory without letting the system display “Are you sure(Y/N)”, you can use the following command: ECHO Y | DEL *.* >NUL In actual use, we can combine ECHO and the pipe symbol reasonably, so that what is displayed on the screen matches our requirements. 4.Setting or resetting the path As the number of application systems increases, 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 specific applications there may sometimes be restrictions. Here we will discuss a solution through an example. Suppose the path we have set is: PATH=C:\DOS;C:\UTILS;C:\BAT And at this time we need to run a word processor, while also needing to set the path to: PATH=C:\DOS;C:\UTILS;C:\BAT;C:\TEXTEDIT After running the above word processor, we also need to restore the path to its original state. Here we can do 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 returns control to the batch program, at which time the path is set back to the original path. In this way 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 specific applications :: and GOTO can be used in place of the REM statement, 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 function can also be achieved with the 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, the proper use of REM, ::, and GOTO will improve the efficiency of batch files. Usually when the comment is short, use :: or GOTO; when the comment is long, use REM. Because REM's execution time is unrelated to the length of the comment, while :: and GOTO will take twice as long to execute when the comment is long. 6.Converting user input to uppercase People who write batch files all know 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 lowercase for these five letters, DOS will consider the IF statement's condition unsatisfied, and thus will not execute the GOTO statement. The solution to this problem is to use the PATH command to convert the lowercase letters in the user's input to 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's input and converts the input to uppercase, the third line sets an environment variable to PATH (that is, the content entered by the user), the fourth line restores PATH to its original 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 is required 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 example below 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 This way, on the first execution, the IF statement on the second line checks whether %1 contains :TASKS:; since on 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:, and this time the condition is satisfied, so LOOPER executes the program section below :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 executing, DOS closes the second LOOPER and returns to the FOR ...IN ...DO statement of the first LOOPER, thereby proceeding to handle the next .TXT file. 8.Hiding command display Many DOS commands display their results, but sometimes because these results are displayed on the screen, the user may be dazzled. For example, with COPY FILE1.TXT FILE2.TXT the screen will display “1 file(s) copied”. 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 ——Excerpted from: Far Sight Information |
|
| Floor2 HR130 | Posted 2003-03-31 00:00 |
| 初级用户 Posts 16 Credits 172 | |
|
Thanks
|
|
| Floor3 zgzjwz | Posted 2004-05-18 00:00 |
| 初级用户 Posts 28 Credits 227 | |
|
Collected.
|
|
| Floor4 willsort | Posted 2004-05-19 00:00 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re lydong:
Good content, but the following points need to be corrected: 1, DIR /A-D | FIND/V “TXT” The article mentions that a space needs to be added before txt to prevent find from filtering out files whose primary filename contains txt; but it overlooks long filenames that begin with txt, so spaces need to be added both before and after txt to avoid find filtering too aggressively. 2, the function of switch /I is to make the FIND command ignore the extension entered on the command line This should be a typo. The switch i makes the FIND command ignore character case, so as to be compatible with extensions entered in different cases. 3, the two cd lines in section 4 Since C:\TEXTEDIT has already been set as a search path, cd C:\TEXTEDIT seems somewhat redundant. And the final cd c:\ is not handled very well either, because it does not restore the path the user was in before execution. 4, REM's execution time is unrelated to the length of the comment, while :: and GOTO will take twice as long to execute when the comment is long This is a fairly widespread mistaken claim. In fact, the opposite is true. When the system interprets ::, upon reaching the second : it determines that this is an invalid batch label name and directly interprets the next line. When interpreting goto, it only checks whether the first character of each line is :, and if not, it directly interprets the next line. But when interpreting rem, it scans the entire line. As long as you add a pipe character for file output into the comment content, you can get a rough look at how they work. 5, IF %USERINPUT%==ERASE GOTO ERASE This is a fairly obvious mistake. The strings on both sides of == do not have quotes or other essential protective characters added. Also, there is a better trick for the implementation in section 6: @echo off But despite these flaws, this is still a good article. |
|
| Floor5 zhri | Posted 2004-05-19 00:00 |
| 高级用户 Posts 153 Credits 665 | |
|
So touching.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |