|
zwq2009
初级用户
 
积分 118
发帖 5
注册 2004-4-10
状态 离线
|
『楼 主』:
批处理文件怎么制作
使用 LLM 解释/回答一下
那位高手知道关于制作批处理文件的教程吗?
Is there any expert who knows about tutorials on making batch files?
|
|
2004-4-10 00:00 |
|
|
老汉
初级用户
 
积分 110
发帖 2
注册 2004-4-11
状态 离线
|
|
2004-4-11 00:00 |
|
|
wphs0326
银牌会员
     爱DOS但不迷DOS
积分 1708
发帖 509
注册 2004-2-25 来自 安徽淮南
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
批处理文件是由一个或一个以上的DOS命令及可执行命令组成的带有扩展名.BAT的文件。当用户以批处理文件名为命令时,DOS会自动依次执行文件中的命令。批处理文件的特点是一次建立可多次执行。
在批处理文件中有一个特殊的批处理文件,每次启动计算机时,系统自动执行该文件中的每一条命令。该文件必须满足两个条件:一是文件名为AUTOEXEC.BAT,二是该文件的位置必须放在启动盘(也可称为系统盘)的根目录下。
在批处理文件中除了使用DOS命令之外,还可使用批处理子命令,这些命令也可看作DOS的内部命令,它们是:
1)ECHO--显示方式设置;其中ECHO ON是使以后的命令在执行前先显示,ECHO OFF是使以后的命令在执行前不显示,ECHO MASSAGE 是不论ECHO的状态为ON或OFF,都显示MESSAGE所指定的信息。
2)REM--注释命令。
3)PAUSE--暂停系统处理,系统显示Press any key to continue…,等待用户按任意一个键后继续执行。
4)GOTO--转向子命令。
5)IF--条件子命令。
6)FOR--循环子命令。
7)SHIFT--改变参数的位置。
电脑每次启动时都会寻找autoexec.bat这条批处理文件,从而可执行一些每次开机都要执行的命令,如设置路径path、加载鼠标驱动mouse、磁盘加速smartdrv等,可以使您的电脑真正自动化。
echo、@、call、pause、rem 是批处理文件最常用的几个命令,我们就从他们开始学起。 echo 表示显示此命令后的字符
echo off 表示在此语句后所有运行的命令都不显示命令行本身
@ 与echo off相象,但它是加在其它命令行的最前面,表示运行时不显示命令行本身。
call 调用另一条批处理文件(如果直接调用别的批处理文件 ,执行完那条文件后将无法执行当前文件后续命令)
pause 运行此句会暂停,显示Press any key to continue... 等待用户按任意键后继续
rem 表示此命令后的字符为解释行,不执行,只是给自己今后查找用的
例:用edit编辑a.bat文件,输入下列内容后存盘为c:\a.bat,执行该批处理文件后可实现:将根目录中所有文件写入 a.txt中,启动UCDOS,进入WPS等功能。
批处理文件的内容为: 文件表示:
echo off 不显示命令行
dir c:\*.* >a.txt 将c盘文件列表写入a.txt
call c:\ucdos\ucdos.bat 调用ucdos
echo 你好 显示"你好"
pause 暂停,等待按键继续
rem 使用wps 注释将使用wps
cd ucdos 进入ucdos目录
wps 使用wps
批处理文件中还可以像C语言一样使用参数,这只需用到一个参数表示符%。
%表示参数,参数是指在运行批处理文件时在文件名后加的字符串。变量可以从 %0到%9,%0表示文件名本身,字符串用%1到%9顺序表示。
例如,C:根目录下一批处理文件名为f.bat,内容为 format %1
则如果执行C:\>f a: 则实际执行的是format a:
又如C:根目录下一批处理文件的名为t.bat,内容为 type %1 type %2
那么运行C:\>t a.txt b.txt 将顺序地显示a.txt和b.txt文件的内容
if goto choice for 是批处理文件中比较高级的命令,如果这几个你用得很熟练,你就是批处理文件的专家啦。
if 表示将判断是否符合规定的条件,从而决定执行不同的命令。 有三种格式:
1、if "参数" == "字符串" 待执行的命令
参数如果等于指定的字符串,则条件成立,运行命令,否则运行下一句。(注意是两个等号)
如if "%1"=="a" format a:
2、if exist 文件名 待执行的命令
如果有指定的文件,则条件成立,运行命令,否则运行下一句。如if exist config.sys edit config.sys
3、if errorlevel 数字 待执行的命令
如果返回码等于指定的数字,则条件成立,运行命令,否则运行下一句。如if errorlevel 2 goto x2 DOS程序运行时都会返回一个数字给DOS,称为错误码errorlevel或称返回码
goto 批处理文件运行到这里将跳到goto 所指定的标号处, 一般与if配合使用。 如:
goto end
:end
echo this is the end
标号用 :字符串 表示,标号所在行不被执行
choice 使用此命令可以让用户输入一个字符,从而运行不同的命令。使用时应该加/c:参数,c:后应写提示可输入的字符,之间无空格。它的返回码为1234……
如: choice /c:dme defrag,mem,end
将显示
defrag,mem,end?
例如,test.bat的内容如下:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag 应先判断数值最高的错误码
if errorlevel 2 goto mem
if errotlevel 1 goto end
:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye
此文件运行后,将显示 defrag,mem,end? 用户可选择d m e ,然后if语句将作出判断,d表示执行标号为defrag的程序段,m表示执行标号为mem的程序段,e表示执行标号为end的程序段,每个程序段最后都以goto end将程序跳到end标号处,然后程序将显示good bye,文件结束。
for 循环命令,只要条件符合,它将多次执行同一命令。
格式FOR in (集合) DO
只要参数f在指定的集合内,则条件成立,执行命令
如果一条批处理文件中有一行:
for %%c in (*.bat *.txt) do type %%c
含义是如果是以bat或txt结尾的文件,则显示文件的内容。
DOS在启动会自动运行autoexec.bat这条文件,一般我们在里面装载每次必用的程序,如: path(设置路径)、smartdrv(磁盘加速)、 mouse(鼠标启动)、mscdex(光驱连接)、 doskey(键盘管理)、set(设置环境变量)等。
如果启动盘根目录中没有这个文件,电脑会让用户输入日期和时间。
例如,一个典型的autoexec.bat内容如下:
@echo off 不显示命令行
prompt $p$g 设置提示符前有目录提示
path c:\dos;c:\;c:\windows;c:\ucdos;c:\tools 设置路径
lh c:\dos\doskey.com 加载键盘管理
lh c:\mouse\mouse.com 加载鼠标管理
lh c:\dos\smartdrv.exe 加载磁盘加速管理
lh c:\dos\mscdex /S /D:MSCD000 /M:12 /V 加载CD-ROM驱动
set temp=c:\temp 设置临时目录
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 starts, 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 known as the system disk).
In addition to using DOS commands in batch files, 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 subsequent commands display before execution, ECHO OFF makes 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 starts can be executed, such as setting the path path, loading the mouse driver mouse, disk acceleration smartdrv, etc., which can make your computer truly automated.
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 execution. 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 reference by yourself.
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:
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 using 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, a batch file in the root directory of drive C is named f.bat, and the content is format %1. Then if you execute C:\>f a:, the actual execution is format a:.
Another example: A batch file in the root directory of drive C is named t.bat, 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 proficient in using 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 will return a number to DOS when running, called the error code errorlevel or return code.
goto The batch file will jump to the label specified by goto when running here. Generally, it is 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. After c:, the characters that can be entered should be written, and there are no spaces between them. Its return code is 1234...
For example: choice /c:dme defrag,mem,end
It will display
defrag,mem,end?
For example, the content of test.bat is as follows:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag should first judge the highest numerical error code
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 is run, it will display defrag,mem,end? The user can choose d, m, e, and then the if statement will make a judgment. d means executing the program segment labeled defrag, m means executing the program segment labeled mem, e means executing the program segment labeled end. Each program segment finally uses 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 in (collection) DO
As long as the parameter f is within 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
The meaning is 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 starting. Generally, we load the programs that are used every time in it, such as: path (set path), smartdrv (disk acceleration), mouse (mouse startup), 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 let the user 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 prompt to have a directory prompt before
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
|

°·.∴▍★∴ 我们的泰坦尼克.....
I l☆ve you!
☆.°·▍▍.☆█☆.°★ 永不会沉没.
◥█▅▅██▅▅██▅▅▅▅███◤
我的主页:http://wphs.ik8.com我的网络硬盘:wphs.ys168.com
Email:wphs@ah163.com QQ:43500498(附加消息:中国DOS联盟)
|
|
2004-4-11 00:00 |
|
|
wphs0326
银牌会员
     爱DOS但不迷DOS
积分 1708
发帖 509
注册 2004-2-25 来自 安徽淮南
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
(一)应用DOS重定向功能
DOS的标准输入输出通常是在标准设备键盘和显示器上进行的, 利用重定向,可以方便地将输入输出改向磁盘文件或其它设备。如在批处理命令执行期间为了禁止命令或程序执行后输出信息而扰乱屏幕, 可用DOS重定向功能把输出改向NUL设备(NUL不指向任何实际设备): C:\>COPY A.TXT B.TXT > NUL。
命令执行结束不显示"1 file(s) copied"的信息。有的交互程序在执行时要求很多键盘输入, 但有时输入是固定不变的, 为加快运行速度, 可预先建立一个输入文件,此文件的内容为程序的键盘输入项, 每个输入项占一行。假如有一个程序ZB, 其输入项全部包括在文件IN.DAT中, 执行 C:\>ZB NUL 程序就自动执行。
(二)应用DOS管道功能
DOS的管道功能是使一个程序或命令的标准输出用做另一个程序或命令的标准输入。如把DEBUG的输入命令写入文件AAA, 用TYPE命令通过管道功能将AAA的内容传输给DEBUG, 在DEBUG执行期间不再从控制台索取命令参数, 从而提高了机器效率。命令为: C:\>TYPE AAA|DEBUG >BBB。
(三)子程序
在一个批处理文件可用CALL命令调用另一个子批处理文件, 当子批文件执行结束后,自动返回父批文件, 继续向下执行。如: A.BAT B.BAT,A调用B,A.BAT内容如下:
@ECHO OFF
CALL B
CD \BASIC
BASICA BG
@ECHO ON
(四)菜单选择功能
DOS功能调用31H或4CH所提供的一字节的返回码, 通过批处理子命令IF和ERRORLEVEL对返回码进行处理, 可达到自动执行一批命令的目的。在批处理文件中实现高级语言所有的菜单提示功能, 使批处理文件变得更灵活方便。先用DEBUG建立一个菜单驱动程序MENU.COM,对应地编写一个批处理文件LG.BAT。具体内容和方法见下表:
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
<img src="images/smilies/face-raspberry.png" align="absmiddle" border="0">AS
CD \TP5.00
TURBO
CD \
GOTO START
:BAS
CD \TB
TB
CD \
GOTO START
<img src="images/smilies/face-raspberry.png" align="absmiddle" border="0">RO
CD \TPROLOG
PROLOG
CD \
GOTO START
:C
CD \TURBOC
TC
CD \
GOTO START
:EX
@ECHO ON
执行LG, 屏幕左上角出现一个菜单, 并提示用户输入选择, 当选择的功能执行结束,重新返回主菜单请求选择, 直到选择"0"号功能, 程序结束返回DOS。
(五)应用命令处理程序完成大量重复工作
DOS提供调用次级命令程序的方法, 可实现与子程序等效的功能, 在MS DOS3.3以前的DOS版本下非常有用。如你有一批FORTRAN源程序需要编译, 首先编写两个批文件MAKEOBJ.BAT、C.BAT, 然后执行MAKEOBJ, 即可把当前目录下的所有扩展名为.FOR的FORTRAN源程序编译成OBJ文件。这种方法迅速正确, 人机交互少, 减轻了程序员的的大量劳动。
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
### (I) Applying DOS Redirection Function
The standard input and output in DOS are usually carried out on the standard devices keyboard and display. By using redirection, the input and output can be conveniently 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 when executing, but sometimes the input is fixed. To speed up the running speed, 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.
### (II) Applying DOS Pipe Function
The pipe function of DOS makes the standard output of one program or command be 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 pipe function. During the execution of DEBUG, it will no longer obtain command parameters from the console, thereby improving the machine efficiency. The command is: C:\>TYPE AAA|DEBUG >BBB.
### (III) Subroutine
A CALL command can be used in a batch file to call another sub-batch file. When the sub-batch file is executed and ends, it will automatically return to the parent batch file and continue to execute downward. For example: A.BAT B.BAT, where A calls B. The content of A.BAT is as follows:
@ECHO OFF
CALL B
CD \BASIC
BASICA BG
@ECHO ON
### (IV) Menu Selection Function
The one-byte return code provided by the DOS function calls 31H or 4CH can be used to process the return code through the batch sub-commands IF and ERRORLEVEL, which can achieve the purpose of automatically executing a batch of commands. 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 will appear in the upper left corner of the screen and prompt the user to enter a choice. When the selected function is executed and ends, it will return to the main menu to request a choice again until the "0" function is selected, and the program will end and return to DOS.
### (V) 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 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, 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
|

°·.∴▍★∴ 我们的泰坦尼克.....
I l☆ve you!
☆.°·▍▍.☆█☆.°★ 永不会沉没.
◥█▅▅██▅▅██▅▅▅▅███◤
我的主页:http://wphs.ik8.com我的网络硬盘:wphs.ys168.com
Email:wphs@ah163.com QQ:43500498(附加消息:中国DOS联盟)
|
|
2004-4-11 00:00 |
|
|
wphs0326
银牌会员
     爱DOS但不迷DOS
积分 1708
发帖 509
注册 2004-2-25 来自 安徽淮南
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
批处理文件应用技巧
虽然Windows 3.x和Windows95应用越来越广泛,但DOS仍然为许多用户所钟爱。就DOS的批处理文件而言,许多用户在实际应用中却没有发挥其应有的作用,本文就DOS批处理文件的应用谈几点技巧,仅供读者参考。
1.显示选择的DIR信息
DOS的DIR命令为了便于用户显示一个目录中特殊类型的文件,提供了开关和通配符的功能。但是DIR命令也有许多不尽人意的方面,例如,如果我们要显示一个目录中除扩展名为.TXT以外的文件,则DIR命令就显得无能为力了。解决的方法是借助FIND命令来实现这一功能:
DIR /A-D | FIND/V “TXT”
“/A-D”开关是告诉DIR命令不要显示目录,管道命令“|”是指定所列目录按FIND命令的执行结果显示,/V开关是告诉FIND命令列出所有不包括双引号内字符串的所有行,双引号内的字符串要以空格为前导(空格是使FIND命令不能排除其所发现的基本文件名中包括字符串TXT的文件,如NEWTXT.DOC,所以空格在这里是不能缺少的)。实际上FIND命令可以在一行命令中多次出现,如显示一个目录中不包括扩展名为.TXT和.DOC的所有文件,命令如下:
DIR /A-D | FIND/V“TXT”FIND/V“DOC”
下面我们给出一个利用上述技巧的实例,该实例用以显示除指定扩展名以外的所有文件,这里假定文件名为NEWDIR,则命令格式为:
NEWDIR
其中扩展名为扩展名1和扩展名2的文件是用户所不想显示的。
@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=
值得注意的是FIND命令后面增加了一个开关/I,这一开关的作用是使FIND命令忽略在命令行中键入扩展名的情况。
2.使输入自动化
在我们编写批处理文件时,有时需要在文件中指定日期,一般而言,我们在批处理文件中加入DATE命令,这样在运行的过程中程序就会提示用户输入日期。这里我们给出一种在DATE命令后面增加回车的方法:
ECHO. | DATE
ECHO. 用于向屏幕上发送一个回车,管道符号用于重定向回车到DATE命令。为了不显示DATE命令的提示,可以用下列命令:
ECHO. | DATE | FIND“Current”
如果想要在文本文件中增加一行日期,可以用下面的命令:
ECHO. | DATE | FIND “Current” >THISDATE.TXT
这种方法同样适合DOS的TIME命令和FORMAT命令。
3.屏蔽显示信息
在批处理文件中,“ECHO”和“|”配合是很有用的,这种配合有时可以消除我们所不希望见到的显示信息。例如在一个目录中删除所有文件,但又不想让系统显示“Are you sure(Y/N)”则可用如下命令:
ECHO Y | DEL *.* >NUL
在实际应用中,我们可以合理地配合使用ECHO和管道符,以使屏幕显示的内容符合我们的要求。
4.设置或重新设置路径
随着应用系统的增多,有些应用需要用户将它们所在的目录加入到PATH语句中,但PATH所支持的字符数不能超过127个字符,这样在具体应用中,有时就可能受到限制。这里我们通过一个例子探讨解决的方法、假设,我们所设置的路径如下:
PATH=C:\DOS;C:\UTILS;C:\BAT
而这时我们需要运行一种字处理软件,同时需要将路径设置成:
PATH=C:\DOS;C:\UTILS;C:\BAT;C:\TEXTEDIT
在运行完上述字处理软件后,还需要将路径设置成原来的样子,这里我们可以通过下面的批处理程序来完成:
@ECHO OFF
SET OLDPATH=%PATH%
PATH %PATH%;C:\TEXTEDIT
CD C:\TEXTEDIT
TEXTEDIT
SET PATH=%OLDPATH%
SET OLDPATH=
CD C:\
批处理程序的第二行是将现行路径保存在环境变量OLDPATH中;第三行合并路径;第六行在系统运行完成后,DOS将控制权交给批处理程序,这时将路径设置回原来的路径。通过这种方法我们就可以解决上述问题。
5.合理使用REM、::、和GOTO
在批处理文件中REM语句的作用众所周知,但在具体应用中可以使用::和GOTO代替REM语句,例如:
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
同样可以使用GOTO语句实现上述功能:
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
一般而言,合理使用REM、::和GOTO将提高批处理文件的使用效率。通常当注释较短时,使用::或GOTO;当注释较长时使用REM。因为REM的执行时间和注释的长短无关,而::和GOTO在注释较长时执行时间将加倍。
6.将用户的输入转换成大写
编写批处理文件的人员都知道,IF语句对于大小写字母是敏感的,例如:
IF“%1”==“ERASE” GOTO ERASE
如果用户键入erase或Erase或者这五个字母的大小写任意组合,DOS将认为IF语句的条件没有满足,进而不执行GOTO语句。解决这一问题的办法是利用PATH命令,将用户输入的小写字母转换成大写,程序如下:
SET OLDPATH=%PATH%
SET PATH=%1
SET USERINPUT=%PATH%
SET PATH=%OLDPATH%
IF %USERINPUT%==ERASE GOTO ERASE
SET OLDPATH=
第一行将现行路径存储在环境变量OLDPATH中,第二行将PATH变量设置成用户输入,并将输入内容转换成为大写,第三行将一个环境变量设置成PATH(即用户输入的内容),第四行还原PATH的原有内容,第五行将转换后的字符串用于IF语句,最后一行清除OLDPATH环境变量。
7.FOR...IN...DO循环
在要求一次执行一个以上命令时,FOR...IN...DO的局限性就显示出来了。例如在批处理文件中,我们不能简单地通过MORE命令显示一个目录中的每一个文件,然后有选择地删除它们。下面的例子演示了克服上述问题的方法:
@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
这里我们将这一批处理程序定名为LOOPER,如果我们要显示所有的文本文件而后给出一个删除选择,则可键入:
LOOPER *.TXT
这样在第一次执行时,第二行的IF语句检查%1是否包含:TASKS:;因为此次执行%1不包含:TASKS:,所以程序运行FOR语句。本例中,程序首先提出第一个扩展名为.TXT的文件而后执行CALL命令。假设第一个文本文件为ABC.TXT,则DOS执行如下命令:
CALL LOOPER :TASKS: ABC.TXT
即LOOPER带两个参数(:TASKS:和ABC.TXT)运行了自身的第二个拷贝。当LOOPER第二次运行时,再次检查%1是否包含值:TASKS:,而此次条件满足,LOOPER执行:TASKS:下面的程序段,MORE显示ABC.TXT的内容,而后DEL /P显示是否删除文件的选择。当:TASKS:例程执行完成,DOS关闭第二个LOOPER并返回第一个LOOPER的FOR ...IN ...DO语句,从而进行下一个.TXT文件的处理。
8.隐藏命令显示
许多DOS命令都要显示他们的结果,但有时由于这些结果显示在屏幕上会使用户眼花缭乱。如COPY FILE1.TXT FILE2.TXT屏幕上会显示“1 file(s) copied”,如果我们不想让系统显示这一信息,则可采用如下方法:
COPY FILE1.TXT FILE2.TXT>NUL
这种方法同样适合于批处理文件的暂停命令PAUSE,可屏蔽PAUSE命令的显示信息“Press any key to continue”。例如:
@ECHO OFF
ECHO There is no disk in drive A,Insert a
ECHO disk and press Enter.
PAUSE>NUL
批命令中的子程序
大多数成熟的高级编程语言允许您把常用的例程建立成独立的模块即子程序,它保
留了代码的独立性,使程序模式化,便于组织.至少可以说DOS的批命令不完全支持子程
序,但在任何时候你都可以用GOTO命令调用子程序.
通常,GOTO命令用的标号还可以接受存储在环境变量中的标号,如:
SET LABELNAME=START
GOTO%LABELNAME%
通过在子程序的开始替换标号并在子程序结束的地方设置GOTO %RETURN%语句,你
可以在DOS的批命令中建立子程序.只要SET 一个环境变量RETURN,你可在批命令的任
意专访调用子程序.例如,你可以给标号赋值并在下一行调用该子程序,招生GOTO语句
跳至该子程序的开始处,程序结构如:
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%
语句GOTO SUB传送到子程序SUB,在SUB中你可以执行任何语句.执行至
GOTO%RETURN%时,批命令返回控制到号HERE,因环境变量RETURN被复赋值为HERE,如果
你
每次调用SUB都 用叭一的标号(如RETURN),则可多次多处调用SUB.
### Batch File Application Tips
Although Windows 3.x and Windows 95 are increasingly widely used, DOS is still favored by many users. As for DOS batch files, many users do not give full play to their due role in practical applications. This article discusses several tips on the application of DOS batch files for readers' reference.
#### 1. Display Selected DIR Information
The DIR command in DOS provides the functions of switches and wildcards to facilitate users to display special types of files in a directory. However, the DIR command also has many unsatisfactory aspects. For example, if we want to display files other than those with the .TXT extension in a directory, 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 directories are 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 in double quotes. The string in double quotes should be preceded by a space (the space is to prevent the FIND command from excluding files whose base filenames include the string TXT, such as NEWTXT.DOC, so the space is indispensable here). In fact, the FIND command can appear multiple times in one line of command. For example, to display all files in a directory that do not include the .TXT and .DOC extensions, the command is as follows:
```
DIR /A-D | FIND/V "TXT" | FIND/V "DOC"
```
Next, we give an example using the above skills. This example is used to display all files except the specified extensions. Here, it is assumed that the file name is NEWDIR, and the command format is:
```
NEWDIR
```
Among them, the files with extensions extension1 and extension2 are the ones that 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 is added after the FIND command. The function of this switch is to make the FIND command ignore the case of the extension typed in the command line.
#### 2. Automate Input
When we write batch files, sometimes we need to specify the date in the file. Generally speaking, we add the DATE command in the batch file, so the program will prompt the user to enter the date during operation. Here we give a method to add 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 not display the prompt of the DATE command, you can use the following command:
```
ECHO. | DATE | FIND "Current"
```
If you want to add a line of date in a text file, you can use the following command:
```
ECHO. | DATE | FIND "Current" >THISDATE.TXT
```
This method is also suitable for the TIME command and FORMAT command of DOS.
#### 3. Shield Display Information
In batch files, the combination of "ECHO" and "|" is very useful. This combination can sometimes eliminate the display information we do not want to see. For example, to delete all files in a directory but not let the system display "Are you sure(Y/N)", you can use the following command:
```
ECHO Y | DEL *.* >NUL
```
In practical applications, we can reasonably combine ECHO and the pipe symbol to make the content displayed on the screen meet our requirements.
#### 4. Set or Reset Path
With the increase of application systems, some applications require users to add their directories to the PATH statement. However, the number of characters supported by PATH cannot exceed 127 characters, so it may sometimes be restricted in specific applications. Here we explore the solution through an example. Suppose the path we set is:
```
PATH=C:\DOS;C:\UTILS;C:\BAT
```
And at this time we need to run a word processing software and need to set the path to:
```
PATH=C:\DOS;C:\UTILS;C:\BAT;C:\TEXTEDIT
```
After running the above word processing software, we also need to set the path back to the original one. Here we can complete it 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 combines the paths; the sixth line after the system runs, DOS hands over the control to the batch program, and at this time sets the path back to the original path. Through this method, we can solve the above problem.
#### 5. Reasonably Use REM, ::, and GOTO
The function of the REM statement in batch files is well known, but in specific applications, :: and GOTO can be used instead 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 be realized using 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, reasonably using REM, ::, and GOTO will improve the efficiency of using batch files. Usually, when the comment is short, use :: or GOTO; when the comment is long, use REM. Because the execution time of REM has nothing to do with the length of the comment, while the execution time of :: and GOTO will double when the comment is long.
#### 6. Convert User Input to Uppercase
People who write batch files know that the IF statement is case-sensitive for uppercase and lowercase letters. For example:
```
IF "%1"=="ERASE" GOTO ERASE
```
If the user types erase or Erase or any combination of uppercase and lowercase of these five letters, DOS will think that the condition of the IF statement is not satisfied, and then not execute the GOTO statement. The solution to this problem is to use the PATH command to convert the user's input lowercase letters 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 content to uppercase. The third line sets an environment variable to PATH (that is, the user's input content). The fourth line restores the original content 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 Loop
The limitation of FOR...IN...DO is revealed when one or more commands need to be executed at a time. 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 the method to overcome 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 option, we can type:
```
LOOPER *.TXT
```
In the first execution, the IF statement in the second line checks whether %1 contains :TASKS:; because %1 does not contain :TASKS: this time, the program runs the FOR statement. In this example, the program first takes out the first file with the .TXT extension 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 its second copy with two parameters (:TASKS: and ABC.TXT). When LOOPER runs for the second time, it checks again whether %1 contains the value :TASKS:, and this time the condition is satisfied. LOOPER executes the program segment below :TASKS:. MORE displays the content of ABC.TXT, and then DEL /P displays the option of whether to delete the file. When the :TASKS: routine is executed, DOS closes the second LOOPER and returns to the FOR ...IN ...DO statement of the first LOOPER to process the next .TXT file.
#### 8. Hide Command Display
Many DOS commands display their results, but sometimes these results displayed on the screen will make users confused. 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 also suitable for the PAUSE command in batch files to shield the display information "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 build commonly used routines into independent modules, that is, subroutines. It retains the independence of the code, makes the program modular, and is convenient for organization. At least it can be said that DOS batch commands do not fully support subroutines, but you can call subroutines with the GOTO command at any time.
Usually, the label used by the GOTO command can also accept the label stored in the environment variable. For example:
```
SET LABELNAME=START
GOTO %LABELNAME%
```
By replacing the label at the beginning of the subroutine and setting the GOTO %RETURN% statement at the end of the subroutine, you can build subroutines in DOS batch commands. As long as you SET an environment variable RETURN, you can call subroutines at any place in the batch command. For example, you can assign a value to the label and call the subroutine in the next line, and then the GOTO statement jumps to the beginning of the subroutine. The program structure is as follows:
```
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 is transferred to the subroutine SUB, and you can execute any statements in SUB. When executing to GOTO %RETURN%, the batch command returns control to the label HERE, because the environment variable RETURN is reassigned to HERE. If you use a unique label (such as RETURN) each time you call SUB, you can call SUB multiple times in multiple places.
|

°·.∴▍★∴ 我们的泰坦尼克.....
I l☆ve you!
☆.°·▍▍.☆█☆.°★ 永不会沉没.
◥█▅▅██▅▅██▅▅▅▅███◤
我的主页:http://wphs.ik8.com我的网络硬盘:wphs.ys168.com
Email:wphs@ah163.com QQ:43500498(附加消息:中国DOS联盟)
|
|
2004-4-11 00:00 |
|
|
wphs0326
银牌会员
     爱DOS但不迷DOS
积分 1708
发帖 509
注册 2004-2-25 来自 安徽淮南
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
以上均为转帖,请原作者不要怪罪。。。
The above are all reprinted posts. Please don't blame the original author...
|

°·.∴▍★∴ 我们的泰坦尼克.....
I l☆ve you!
☆.°·▍▍.☆█☆.°★ 永不会沉没.
◥█▅▅██▅▅██▅▅▅▅███◤
我的主页:http://wphs.ik8.com我的网络硬盘:wphs.ys168.com
Email:wphs@ah163.com QQ:43500498(附加消息:中国DOS联盟)
|
|
2004-4-11 00:00 |
|
|
eboyzwb
初级用户
 
积分 185
发帖 21
注册 2004-4-23
状态 离线
|
|
2004-4-24 00:00 |
|
|
hendry2003
初级用户
 
积分 192
发帖 20
注册 2004-3-22 来自 上海市
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
请问第2楼的大虾,FOR不是批处理文件的专用的命令吗,你在下面举的例子里怎么该成了FOR1了呢,小弟石头一个,能给我说以下吗?谢谢了。
Excuse me, senior at the 2nd floor, isn't FOR a special command for batch files? Why did you change it to FOR1 in the example below? I'm a novice, can you explain it to me? Thanks.
|

**************%%%%%%%%%************
我是大菜鸟,渴望成为一个能独立飞翔的老鸟
请各位大 虾不吝赐教,小弟在此谢谢了。
**************%%%%%%%**************** |
|
2004-4-25 00:00 |
|
|
jjwj
初级用户
 
积分 163
发帖 18
注册 2003-10-29
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
棒棒棒,我很菜,想老大学习
Great! I'm quite inexperienced. I want to learn from the senior.
|
|
2004-5-14 00:00 |
|
|
laochang409
中级用户
  
积分 297
发帖 44
注册 2004-6-15
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
非常感谢!回去慢慢消化。
Thank you very much! Go back and digest it slowly.
|
|
2004-6-18 00:00 |
|
|