所有的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批处理语法总结 供大家分享,谢谢,很强大哦
【批处理语法详解】 批处理,又称批处理脚本,是一种在DOS和Windows系统中广泛使用的自动化工具。它的核心是通过编写简单的命令脚本来批量执行任务,从而提高工作效率。批处理文件通常以`.bat`或`.cmd`为扩展名,...
经典BAT批处理文件语法教程及使用方法 本文将详细讲述BAT批处理文件的语法、使用方法和编程技术。BAT批处理文件是一种文本文件,每一行都是一个DOS命令,可以使用DOS下的Edit或Windows的记事本等文本编辑工具创建和...
根据给定文件的信息,我们可以总结出关于批处理脚本语法的关键知识点。 ### 一、批处理文件的本质 批处理文件本质上是一个包含了一系列DOS命令的文本文件。它允许用户通过一条命令来执行多个预定义的任务,从而...
### 批处理语法详解 #### 一、批处理概述 批处理是一种计算机脚本技术,它将一系列命令封装在一个文本文件中,以便批量执行。这种技术广泛应用于Windows操作系统中,通常使用`.bat`或`.cmd`作为文件扩展名。通过...
### 批处理FOR循环语法详解 #### 一、概述 批处理中的`FOR`命令是一种常用的循环结构,它能够帮助用户高效地执行一系列任务。在Windows操作系统中,尤其是在DOS环境下,`FOR`循环被广泛应用于脚本编写中,以实现...
#### 三、批处理语法格式 批处理的基本语法结构较为简单,主要由命令组成,每条命令独立执行。具体来说: - **命令行格式**:批处理中的每一行可以包含一条或多条命令。 - **子命令**:单条命令内部可以包含多个子...
这本“Windows批处理教程电子书”旨在深入浅出地介绍批处理的基本概念、语法和实际应用,帮助读者掌握这一实用技术。 **1. 批处理基础** 批处理文件是文本文件,包含了DOS命令或其他操作系统命令,如Windows ...
### BAT批处理脚本语法教程知识点详解 #### 一、基础语法 - **批处理文件扩展名**:批处理文件通常以`.bat`或`.cmd`作为扩展名。 - **执行方式**:创建一个批处理文件后,可以通过双击文件图标来运行它,或者在...
总之,Windows命令行和批处理语法提供了强大的自动化和系统管理工具。熟练掌握这些工具可以帮助用户更有效地执行各种任务,尤其是在需要批量操作或无需图形用户界面的情况下。随着PowerShell的出现,自动化和脚本...
7. **批处理详细教程.pdf**:这个文件很可能包含如何创建和使用批处理的详细步骤,包括各种DOS命令的使用方法、批处理语法、高级技巧以及实例解析。 8. **1.txt**:这可能是一个示例批处理脚本,或者记录了某些与...
**Windows命令行(批处理)语法全解** Windows操作系统提供了两种命令行shell,分别是Command Shell和PowerShell。Command Shell是Windows内置的首个shell,主要用于通过批处理(.bat)文件自动化日常任务,如用户账户...
2. **批处理语法**:批处理文件内部使用控制结构,如`IF`条件语句,`FOR`循环,`GOTO`跳转,以及`PAUSE`暂停脚本执行等,使得脚本具有一定的逻辑性。例如,`IF %ERRORLEVEL%==0`检查上一条命令是否成功,`FOR /F`则...
2. **批处理语法**:批处理脚本以`.bat`或`.cmd`为扩展名,使用文本编辑器创建。每行被视为一条命令,可以包含命令、参数和命令行选项。批处理文件中的注释以`REM`开始。 3. **变量与环境变量**:批处理支持内部...
2. **批处理语法**:批处理脚本以`.bat`或`.cmd`为扩展名,其中包含了命令和指令。比如`@echo off`用于关闭命令提示符的回显,`pause`则在脚本执行后暂停,等待用户输入。学习批处理语法,理解如何组织命令和指令,...
总的来说,批处理文件是Windows环境下强大的自动化工具,通过掌握基本的批处理语法和技巧,可以创建出解决各种实际问题的脚本。不过,随着 PowerShell 和其他现代脚本语言的出现,批处理在某些场景下可能已被替代,...
接着,深入学习批处理语法,包括变量的使用(%var%)、条件语句(if、else)、循环语句(for、do)、子批处理调用(call)等。此外,批处理还支持错误处理(errorlevel)和函数(label)等高级特性。 批处理标准...
3. **批处理语法**:包括变量的使用(例如 `%var%`),条件语句(如`if`、`else`)、循环结构(如`for`循环)以及批处理内部命令(如`call`、`echo`、`set`等)。 4. **批处理高级技巧**:如何在批处理中使用外部...
#### 四、批处理语法详解 - **条件判断** - `if`:根据不同的条件执行相应的命令。 ```batch if condition1 command1 if condition2 command2 ``` - `if /i`:不区分大小写的条件判断。 - `if exist`:检查...