Batch File Application Tips
Although Windows 3.x and Windows95 applications are becoming more and more widespread, DOS is still loved by many users. As for DOS batch files, many users have not brought out their proper role in actual use. This article discusses a few tips on applying DOS batch files, for readers' reference only.
1.Displaying selected DIR information
To make it convenient for users to 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 the files in a directory except those with the extension .TXT, then 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 contents be 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 quotation marks. The string inside the double quotation marks must be preceded by a space (the space keeps the FIND command from excluding files whose base filename includes 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 that do not include 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 the specified extensions. Here we assume the filename is NEWDIR, and the command format is:
NEWDIR
The files whose extensions are 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 typed 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, 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. To keep the DATE command prompt from being displayed, 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.Masking displayed information
In batch files, combining “ECHO” with “|” is very useful. This combination can sometimes eliminate display 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 reasonably combine ECHO and the pipe symbol so that the contents displayed on the screen meet our requirements.
4.Setting or resetting 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 specific applications this may sometimes be limited. Here we discuss a solution through an example. Suppose the path we have set is as follows:
PATH=C:\DOS;C:\UTILS;C:\BAT
And at this time we need to run a word-processing software tool, 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 software tool, we still need to set the path back to its original form. Here we can complete 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; in the sixth line, after the system finishes running, DOS returns control to the batch program, and at this point the path is set back to the original path. Through this method we can solve the above problem.
5.Proper use of REM, ::, and GOTO
The role of the REM statement in batch files is well known, but in actual use :: and GOTO can be used to replace 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 GOTO statement can likewise be used to achieve the above function:
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. Because REM's execution time is unrelated to the length of the comments, while the execution time of :: and GOTO will double when the comments are long.
6.Converting the user's input to uppercase
People who write batch files all know that IF statements are sensitive to the case of letters, for example:
IF“%1”==“ERASE” GOTO ERASE
If the user types erase or Erase or any upper/lowercase combination of these five letters, DOS will consider that the IF statement's condition has not been met, and therefore will not execute the GOTO statement. The way to solve this problem 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's input and converts the input content to uppercase. The third line sets an environment variable to PATH (that is, the user's input). The fourth line restores the original contents of PATH. The fifth line uses the converted string in the IF statement. The last line clears the OLDPATH environment variable.
7.FOR...IN...DO loops
When more than one command is required to be executed at once, the limitations of FOR...IN...DO show up. For example, in a batch file, we cannot simply display each file in a directory through the MORE command and then selectively delete them. The following example demonstrates a method for overcoming the above 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 deletion choice, we can type:
LOOPER *.TXT
In this way, during the first execution, the IF statement on the second line checks whether %1 contains :TASKS:; because this execution's %1 does not contain :TASKS:, the program runs the FOR statement. In this example, the program first brings up the first file with 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 met. 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 executing, DOS closes the second LOOPER and returns to the first LOOPER's FOR ...IN ...DO statement, thereby processing the next .TXT file.
8.Hiding command display
Many DOS commands display their results, but sometimes these results shown on the screen can dazzle the user. 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 information, we can use the following method:
COPY FILE1.TXT FILE2.TXT>NUL
This method is likewise suitable for the batch file pause command PAUSE, and can mask the PAUSE command's display 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
Although Windows 3.x and Windows95 applications are becoming more and more widespread, DOS is still loved by many users. As for DOS batch files, many users have not brought out their proper role in actual use. This article discusses a few tips on applying DOS batch files, for readers' reference only.
1.Displaying selected DIR information
To make it convenient for users to 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 the files in a directory except those with the extension .TXT, then 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 contents be 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 quotation marks. The string inside the double quotation marks must be preceded by a space (the space keeps the FIND command from excluding files whose base filename includes 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 that do not include 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 the specified extensions. Here we assume the filename is NEWDIR, and the command format is:
NEWDIR
The files whose extensions are 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 typed 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, 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. To keep the DATE command prompt from being displayed, 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.Masking displayed information
In batch files, combining “ECHO” with “|” is very useful. This combination can sometimes eliminate display 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 reasonably combine ECHO and the pipe symbol so that the contents displayed on the screen meet our requirements.
4.Setting or resetting 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 specific applications this may sometimes be limited. Here we discuss a solution through an example. Suppose the path we have set is as follows:
PATH=C:\DOS;C:\UTILS;C:\BAT
And at this time we need to run a word-processing software tool, 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 software tool, we still need to set the path back to its original form. Here we can complete 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; in the sixth line, after the system finishes running, DOS returns control to the batch program, and at this point the path is set back to the original path. Through this method we can solve the above problem.
5.Proper use of REM, ::, and GOTO
The role of the REM statement in batch files is well known, but in actual use :: and GOTO can be used to replace 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 GOTO statement can likewise be used to achieve the above function:
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. Because REM's execution time is unrelated to the length of the comments, while the execution time of :: and GOTO will double when the comments are long.
6.Converting the user's input to uppercase
People who write batch files all know that IF statements are sensitive to the case of letters, for example:
IF“%1”==“ERASE” GOTO ERASE
If the user types erase or Erase or any upper/lowercase combination of these five letters, DOS will consider that the IF statement's condition has not been met, and therefore will not execute the GOTO statement. The way to solve this problem 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's input and converts the input content to uppercase. The third line sets an environment variable to PATH (that is, the user's input). The fourth line restores the original contents of PATH. The fifth line uses the converted string in the IF statement. The last line clears the OLDPATH environment variable.
7.FOR...IN...DO loops
When more than one command is required to be executed at once, the limitations of FOR...IN...DO show up. For example, in a batch file, we cannot simply display each file in a directory through the MORE command and then selectively delete them. The following example demonstrates a method for overcoming the above 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 deletion choice, we can type:
LOOPER *.TXT
In this way, during the first execution, the IF statement on the second line checks whether %1 contains :TASKS:; because this execution's %1 does not contain :TASKS:, the program runs the FOR statement. In this example, the program first brings up the first file with 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 met. 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 executing, DOS closes the second LOOPER and returns to the first LOOPER's FOR ...IN ...DO statement, thereby processing the next .TXT file.
8.Hiding command display
Many DOS commands display their results, but sometimes these results shown on the screen can dazzle the user. 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 information, we can use the following method:
COPY FILE1.TXT FILE2.TXT>NUL
This method is likewise suitable for the batch file pause command PAUSE, and can mask the PAUSE command's display 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
我的网志
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
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
