所有的MS-DOS命令都可以放在命令行中执行,也可以放在一个批处理文件中执行。
另外,下面这些命令是专门为批处理文件提供的:
<Call> <If>
<Choice> <Pause>
<Echo> <Rem>
<For> <Shift>
<Goto>
你可以使用COMMAND /Y命令来一步一步的执行批处理文件,并且可以选择跳过
或执行一条命令,这个可以方便批处理程序的调试。下面列出了以上各命令的
使用说明。
参数和变量:
批处理的参数几乎可以是任何内容,在批处理中使用%1到%9来代替输入的参数,
变量的表达方式为%var%,如:%baud%
CALL使用说明:
用来从一个批处理程序中调用另一个批处理程序,且不停止发出调用的
批处理程序的执行
使用语法:
CALL [drive:][path]filename [batch-parameters]
参数说明:
[drive:][path]filename
批处理文件所在路径和文件名,文件名必须是以.BAT为扩展名的文件
batch-parameters
命令行参数
使用参数:
批处理的参数几乎可以是任何内容,在批处理中使用%1到%9来代替输入的参数,
环境变量的表达方式为%var%,如:%baud%
使用管道和重定向符号:
在使用CALL命令时,不可以使用管道符"|"和重定向符"<<","<",">",">>"
使用递归:
使用CALL调用批处理本身,达到递归调用的效果,但是必须要有一个条件让批
处理结束
IF使用说明:
使用方法与C语言中的if用法类似,IF命令只可以在批处理中使用
使用语法:
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
参数说明:
NOT
逻辑非
ERRORLEVEL number
如果上一个程序的返回码大于或等于number,则返回真
string1==string2
如果string1与string2相同,则返回真
EXIST filename
如果filename存在,则返回真
command
如果IF返回真,则执行command
CHOICE使用说明:
只能在批处理中使用,暂停批处理程序执行,让用户选择,在Windows2000版
及以后的DOS环境中已经不提供这个命令
使用语法:
CHOICE [/C[:]keys] [/N] [/S] [/T[:]c,nn] [text]
参数说明:
text
在提供选择时,显示的提示内容,必须用引号括起
如:Are you sure to delete the file [Y/N]?
其中的"Are you sure to delete the file"就是text
指定的内容
选项:
/C[:]keys
指定选项,显示时各选项通过逗号分隔,放在一对中括弧中[],
后面紧跟一个问号,如果不指定keys,CHOICE使用默认的YN,
其中的":"是可有可无的
/N
使得CHOICE不显示提示,但是text仍然显示
/S
使得CHOICE大小写敏感,如果没有/S选项,CHOICE是不区分大小写的
/T[:]c,nn
指定当用户停顿nn秒后,使用默认的c作为用户的输入,nn从0到99
0表示不停顿
PAUSE使用说明:
只能在批处理中使用,暂停批处理的执行,并给出一段提示"按任意键继续"
如果按CTRL+C,则提示"终止批处理操作吗[Y/N]?"
如果选择Y,则停止执行批处理任务。
显示的内容根据不同语言版本的操作系统,显示的内容不同
ECHO使用说明:
使用语法:
ECHO [ON|OFF]
阻止或允许批处理调用的命令显示信息,如:在批处理中调用copy命令,
在执行的时候会显示copy...到屏幕,如果设置了echo off,则不显示。
不带参数使用echo命令,显示当前的状态ON或OFF
echo [message]
显示message,如果需要打印多行,可以使用多条echo命令。无论ECHO
当前状态是ON或OFF,message都会被显示出来。因为不带参数的ECHO命令
显示当前的ECHO状态,所以要显示一个空行,可以使用命令
ECHO.
如果不想批处理中使用的命令显示在命令行上,可以在命令之前加一个"@"符
REM使用说明:
在屏幕上显示信息,可以作为注释行使用,如果ECHO OFF,则REM不显示内容
FOR使用说明:
与C语言中的for类似
使用语法:
FOR %%variable IN (set) DO command [command-parameters]
参数说明:
(set)
是一个集合,如:*.BAT *.DOC d*.txt ?d*a*.t?t
%%variable
是集合中的一个成员,如*.BAT中的test.bat,FOR每次从集合中取一个
成员
IN,DO
不是参数,但是在FOR中必须用到,属于格式的一部分,如果缺少,MS-DOS
将会显示错误信息
command [command-parameters]
每次循环中执行的命令和命令中使用的参数
使用举例:
显示所有文件名第一个字母是A为txt文件
for %filename in (A*.txt) do type %filename
将所有文件名第二个字母是A为txt文件打印到打印机
for %filename in (A*.txt) do type %filename > prn
在MS-DOS中打印机使用prn表示,>符表示重定向
SHIFT使用说明:
批处理程序一次最多能接受十个参数,分别用%0到%9表示,当超过十个参数时,
可以使用SHIFT命令来处理,一次将参数前移一次,即:参数%1的值被赋给%0,
%2的值被赋给%1,一次类推,%0原来的值被丢弃,无法还原
使用举例:
set todir=%1
:getfile
shift
if "%1"=="" goto end
copy %1 %todir%
goto getfile
:end
set todir=
echo All done
GOTO使用方法:
因为FOR一次只能执行一条命令,所以在一次循环中需要执行多条命令时,
需要使用GOTO,GOTO的语法与C语言的语法一样
附录(WINDOWS2000 DOS命令列表):
使用HELP COMMAND查看更详细的帮助信息,如:help at
ASSOC Displays or modifies file extension associations
AT Schedules commands and programs to run on a computer.
ATTRIB Displays or changes file attributes.
BREAK Sets or clears extended CTRL+C checking.
CACLS Displays or modifies access control lists (ACLs) of files.
CALL Calls one batch program from another.
CD Displays the name of or changes the current directory.
CHCP Displays or sets the active code page number.
CHDIR Displays the name of or changes the current directory.
CHKDSK Checks a disk and displays a status report.
CHKNTFS Displays or modifies the checking of disk at boot time.
CLS Clears the screen.
CMD Starts a new instance of the Windows 2000 command interpreter.
COLOR Sets the default console foreground and background colors.
COMP Compares the contents of two files or sets of files.
COMPACT Displays or alters the compression of files on NTFS partitions.
CONVERT Converts FAT volumes to NTFS. You cannot convert the
current drive.
COPY Copies one or more files to another location.
DATE Displays or sets the date.
DEL Deletes one or more files.
DIR Displays a list of files and subdirectories in a directory.
DISKCOMP Compares the contents of two floppy disks.
DISKCOPY Copies the contents of one floppy disk to another.
DOSKEY Edits command lines, recalls Windows 2000 commands, and creates macros.
ECHO Displays messages, or turns command echoing on or off.
ENDLOCAL Ends localization of environment changes in a batch file.
ERASE Deletes one or more files.
EXIT Quits the CMD.EXE program (command interpreter).
FC Compares two files or sets of files, and displays the differences
between them.
FIND Searches for a text string in a file or files.
FINDSTR Searches for strings in files.
FOR Runs a specified command for each file in a set of files.
FORMAT Formats a disk for use with Windows 2000.
FTYPE Displays or modifies file types used in file extension associations.
GOTO Directs the Windows 2000 command interpreter to a labeled line in a
batch program.
GRAFTABL Enables Windows 2000 to display an extended character set in graphics
mode.
HELP Provides Help information for Windows 2000 commands.
IF Performs conditional processing in batch programs.
LABEL Creates, changes, or deletes the volume label of a disk.
MD Creates a directory.
MKDIR Creates a directory.
MODE Configures a system device.
MORE Displays output one screen at a time.
MOVE Moves one or more files from one directory to another directory.
PATH Displays or sets a search path for executable files.
PAUSE Suspends processing of a batch file and displays a message.
POPD Restores the previous value of the current directory saved by PUSHD.
PRINT Prints a text file.
PROMPT Changes the Windows 2000 command prompt.
PUSHD Saves the current directory then changes it.
RD Removes a directory.
RECOVER Recovers readable information from a bad or defective disk.
REM Records comments (remarks) in batch files or CONFIG.SYS.
REN Renames a file or files.
RENAME Renames a file or files.
REPLACE Replaces files.
RMDIR Removes a directory.
SET Displays, sets, or removes Windows 2000 environment variables.
SETLOCAL Begins localization of environment changes in a batch file.
SHIFT Shifts the position of replaceable parameters in batch files.
SORT Sorts input.
START Starts a separate window to run a specified program or command.
SUBST Associates a path with a drive letter.
TIME Displays or sets the system time.
TITLE Sets the window title for a CMD.EXE session.
TREE Graphically displays the directory structure of a drive or path.
TYPE Displays the contents of a text file.
VER Displays the Windows 2000 version.
VERIFY Tells Windows 2000 whether to verify that your files are written
correctly to a disk.
VOL Displays a disk volume label and serial number.
XCOPY Copies files and directory trees.
分享到:
相关推荐
bat批处理语法总结 供大家分享,谢谢,很强大哦
script grammer 批处理 语法
系统的讲解批处理语法(bat)的教程,doc格式,使用word打开
批处理语法,bat语法 批处理就是把一批或者说是一条条命令放在一个文本里,然后批量执行!执行这 一批命令的文件的扩展名是BAT文件或者CMD,把任何一批命令放入在有这样扩展名的文件里,执行时里面的命令就会一条条的...
经典BAT批处理文件语法教程及使用方法.doc bat的入门教程。
批处理 FOR的语法,很好很详细的说明,其中有很多实例,不可能看了还不会的
bat批处理脚本语法教程(超经典) 一、基础语法: 二、参数: 三、批处理基本命令 四、其它命令 五、字符串处理 六、注册表操作 七、系统服务 八、setlocal与变量延迟 九、文件处理 十、实践实例(1,2,3)
BAT批处理文件语法.rar BAT批处理文件语法.rar
bat_批处理_语法 批处理 技巧 语法
批处理文件语法备忘,BAT文件语法和技巧
.bat是dos下的批处理文件 .cmd是nt内核命令行环境的另一种批处理文件 从更广义的角度来看,unix的shell脚本以及其它操作系统甚至应用程序中由外壳进行解释执行的文本,都具有与批处理文件十分相似的作用,而且同样...
BAT批处理文件语法之常用命令和参数
Windows命令行(批处理)语法全解,详细讲解批处理脚本语法, 包含目录,可以快速查询,批处理任务书写不再东翻西找。 Windows 有两个命令行 shell:Command shell 和 PowerShell。 每个 shell 都是一个软件 程序,它...
BAT批处理文件语法,扩展名是bat(在nt/2000/xp/2003下也可以是cmd)的文件就是批处理文件。
介绍批处理语言 Windows命令用法 批处理的简介 批处理语法 常见命令
介绍批处理语法,迅速成为高手