Batch file writing simple manual

 

First of all, the batch file is a text file, each line of this file is a DOS command (most of the time it is just like the command line we execute at the DOS prompt), you can use Edit under DOS or Windows Create and modify batch files with any text file editing tool like notepad.

Secondly, a batch file is a simple program that can control the flow of command execution through conditional statements (if) and flow control statements (goto). In batch processing, a loop statement (for) can also be used to execute a command in a loop. . Of course, the programming capabilities of batch files are very limited and non-standard compared to programming statements such as C language. Batch program statements are DOS commands (including internal commands and external commands), and the ability of batch processing mainly depends on the commands you use.

Third, every prepared batch file is equivalent to a DOS external command, and you can put its directory in your DOS search path (path) to make it run anywhere. A good habit is to create a bat or batch directory on the hard disk (eg C:/BATCH), and then put all the batch files you write in this directory, so as long as you set c:/batch in the path, you You can run all the batch programs you write anywhere.

Fourth, under DOS and Win9x/Me systems, the AUTOEXEC.BAT batch file in the root directory of the C: disk is an automatic running batch file, which will run automatically every time the system starts. Put the commands that need to be run at all times into this file, such as setting the search path, transferring the mouse driver and disk cache, setting system environment variables, etc. The following is an example of autoexec.bat running under Windows 98:

@ECHO OFF

PATH C:/WINDOWS;C:/WINDOWS/COMMAND;C:/UCDOS;C:/DOSTools;C:/SYSTOOLS;C:/WINTOOLS;C:/BATCH

LH SMARTDRV.EXE /X

LH DOSKEY.COM /INSERT

LH CTMOUSE.EXE

SET TEMP=D:/TEMP

SET TMP=D:/TEMP

 

The role of batch processing

简单的说,批处理的作用就是自动的连续执行多条命令。

这里先讲一个最简单的应用:在启动wps软件时,每次都必须执行(>前面内容表示DOS提示符):

C:/>cd wps

C:/WPS>spdos

C:/WPS>py

C:/WPS>wbx

C:/WPS>wps

如果每次用WPS之前都这样执行一遍,您是不是觉得很麻烦呢?

好了,用批处理,就可以实现将这些麻烦的操作简单化,首先我们编写一个runwps.bat批处理文件,内容如下:

@echo off

c:

cd/wps

spdos

py

wbx

wps

cd/

以后,我们每次进入wps,只需要运行runwps这个批处理文件即可。

 

常用命令

echo、@、call、pause、rem(小技巧:用::代替rem)是批处理文件最常用的几个命令,我们就从他们开始学起。

echo 表示显示此命令后的字符

echo off 表示在此语句后所有运行的命令都不显示命令行本身

@与echo off相象,但它是加在每个命令行的最前面,表示运行时不显示这一行的命令行(只能影响当前行)。

call 调用另一个批处理文件(如果不用call而直接调用别的批处理文件,那么执行完那个批处理文件后将无法返回当前文件并执行当前文件的后续命令)。

pause 运行此句会暂停批处理的执行并在屏幕上显示Press any key to continue...的提示,等待用户按任意键后继续

rem 表示此命令后的字符为解释行(注释),不执行,只是给自己今后参考用的(相当于程序中的注释)。

例1:用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语言的函数一样使用参数(相当于DOS命令的命令行参数),这需要用到一个参数表示符"%"。

%[1-9]表示参数,参数是指在运行批处理文件时在文件名后加的以空格(或者Tab)分隔的字符串。变量可以从%0到%9,%0表示批处理命令本身,其它参数字符串用%1到%9顺序表示。

例2:C:根目录下有一批处理文件名为f.bat,内容为:

@echo off

format %1

如果执行C:/>f a:

那么在执行f.bat时,%1就表示a:,这样format %1就相当于format a:,于是上面的命令运行时实际执行的是format a:

例3:C:根目录下一批处理文件名为t.bat,内容为:

@echo off

type %1

type %2

那么运行C:/>t a.txt b.txt

%1 : 表示a.txt

%2 : 表示b.txt

于是上面的命令将顺序地显示a.txt和b.txt文件的内容。

 

特殊命令

if goto choice for是批处理文件中比较高级的命令,如果这几个你用得很熟练,你就是批处理文件的专家啦。

一、if 是条件语句,用来判断是否符合规定的条件,从而决定执行不同的命令。 有三种格式:

1、if [not] "参数" == "字符串" 待执行的命令

参数如果等于(not表示不等,下同)指定的字符串,则条件成立,运行命令,否则运行下一句。

例:if "%1"=="a" format a:

2、if [not] exist [路径/]文件名 待执行的命令

如果有指定的文件,则条件成立,运行命令,否则运行下一句。

如: if exist c:/config.sys type c:/config.sys

表示如果存在c:/config.sys文件,则显示它的内容。

3、if errorlevel <数字> 待执行的命令

很多DOS程序在运行结束后会返回一个数字值用来表示程序运行的结果(或者状态),通过if errorlevel命令可以判断程序的返回值,根据不同的返回值来决定执行不同的命令(返回值必须按照从大到小的顺序排列)。如果返回值等于指定的数字,则条件成立,运行命令,否则运行下一句。

如if errorlevel 2 goto x2

二、goto 批处理文件运行到这里将跳到goto所指定的标号(标号即label,标号用:后跟标准字符串来定义)处,goto语句一般与if配合使用,根据不同的条件来执行不同的命令组。

如:

goto end

:end

echo this is the end

标号用":字符串"来定义,标号所在行不被执行。

三、choice 使用此命令可以让用户输入一个字符(用于选择),从而根据用户的选择返回不同的errorlevel,然后于if errorlevel配合,根据用户的选择运行不同的命令。

注意:choice命令为DOS或者Windows系统提供的外部命令,不同版本的choice命令语法会稍有不同,请用choice /?查看用法。

choice的命令语法(该语法为Windows 2003中choice命令的语法,其它版本的choice的命令语法与此大同小异):

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

描述:

该工具允许用户从选择列表选择一个项目并返回所选项目的索引。

参数列表:

/C choices 指定要创建的选项列表。默认列表是 "YN"。

/N         在提示符中隐藏选项列表。提示前面的消息得到显示,选项依旧处于启用状态。

/CS 允许选择分大小写的选项。在默认情况下,这个工具是不分大小写的。

/T timeout 做出默认选择之前,暂停的秒数。可接受的值是从 0 到 9999。如果指定了 0,就不会有暂停,默认选项

           会得到选择。

/D choice    在 nnnn 秒之后指定默认选项。字符必须在用 /C 选项指定的一组选择中; 同时,必须用 /T 指定 nnnn。

/M text     指定提示之前要显示的消息。如果没有指定,工具只显示提示。

/?         显示帮助消息。

 注意:

ERRORLEVEL 环境变量被设置为从选择集选择的键索引。列出的第一个选择返回 1,第二个选择返回 2,等等。如果用户按的键不是有效的选择,该工具会发出警告响声。如果该工具检测到错误状态,它会返回 255 的ERRORLEVEL 值。如果用户按 Ctrl+Break 或 Ctrl+C 键,该工具会返回 0 的 ERRORLEVEL 值。在一个批程序中使用 ERRORLEVEL 参数时,将参数降序排列。

示例:

CHOICE /?

CHOICE /C YNC /M "确认请按 Y,否请按 N,或者取消请按 C。"

CHOICE /T 10 /C ync /CS /D y

CHOICE /C ab /M "选项 1 请选择 a,选项 2 请选择 b。"

CHOICE /C ab /N /M "选项 1 请选择 a,选项 2 请选择 b。"

如果我运行命令:CHOICE /C YNC /M "确认请按 Y,否请按 N,或者取消请按 C。"

屏幕上会显示:

确认请按 Y,否请按 N,或者取消请按 C。 [Y,N,C]?

例:test.bat的内容如下(注意,用if errorlevel判断返回值时,要按返回值从高到低排列):

@echo off

choice /C dme /M "defrag,mem,end"

if errorlevel 3 goto end

if errorlevel 2 goto mem

if errotlevel 1 goto defrag

:defrag

c:/dos/defrag

goto end

:mem

mem

goto end

:end

echo good bye

此批处理运行后,将显示"defrag,mem,end[D,M,E]?" ,用户可选择d m e ,然后if语句根据用户的选择作出判断,d表示执行标号为defrag的程序段,m表示执行标号为mem的程序段,e表示执行标号为end的程序段,每个程序段最后都以goto end将程序跳到end标号处,然后程序将显示good bye,批处理运行结束。

四、for 循环命令,只要条件符合,它将多次执行同一命令

语法:

对一组文件中的每一个文件执行某个特定命令。

FOR %%variable IN (set) DO command [command-parameters]

%%variable    指定一个单一字母可替换的参数。

(set)      指定一个或一组文件。可以使用通配符。

command     指定对每个文件执行的命令。

command-parameters 为特定命令指定参数或命令行开关。

例如一个批处理文件中有一行:

for %%c in (*.bat *.txt) do type %%c

则该命令行会显示当前目录下所有以bat和txt为扩展名的文件的内容。

 

 

###########################

1. 所有内置命令的帮助信息

###########################

ver

cmd /?

set /?

rem /?

if /?

echo /?

goto /?

for /?

shift /?

call /?

其他需要的常用命令

type /?

find /?

findstr /?

copy /?

 

#############################

2. 环境变量的概念

#############################

对环境变量的引用使用(英文模式,半角)双引号

%windir% 变量

%%windir%% 二次变量引用.

我们常用的还有

%temp% 临时文件目录

%windir% 系统目录

%errorlevel% 退出代码

输出文件到临时文件目录里面.这样便于当前目录整洁.

 

############################################

3. 内置的特殊符号(实际使用中间注意避开)

############################################

微软里面内置了下列字符不能够在创建的文件名中间使用

con nul aux / / | || && ^ > < *

You can use most characters as variable values, including white space. If you use the special characters <, >, |, &, or ^, you must precede them with the escape character (^) or quotation marks. If you use quotation marks, they are included as part of the value because everything following the equal sign is taken as the value. Consider the following examples:

(大意: 要么你使用^作为前导字符表示.或者就只有使用双引号""了)

To create the variable value new&name, type:

set varname=new^&name

To create the variable value "new&name", type:

set varname="new&name"

The ampersand (&), pipe (|), and parentheses ( ) are special characters that must be preceded by the escape character (^) or quotation marks when you pass them as arguments.

find "Pacific Rim" < trade.txt > nwtrade.txt

IF EXIST filename. (del filename.) ELSE echo filename. missing

> 创建一个文件

>> 追加到一个文件后面

@ 前缀字符.表示执行时本行在cmd里面不显示, 可以使用 echo off关闭显示

^ 对特殊符号( > < &)的前导字符. 第一个只是显示aaa 第二个输出文件bbb

echo 123456 ^> aaa

echo 1231231 > bbb

() 包含命令

(echo aa & echo bb)

, 和空格一样的缺省分隔符号.

; 注释,表示后面为注释

: 标号作用

| 管道操作

& Usage:第一条命令 & 第二条命令 [& 第三条命令...]

用这种方法可以同时执行多条命令,而不管命令是否执行成功

dir c:/*.exe & dir d:/*.exe & dir e:/*.exe

&& Usage:第一条命令 && 第二条命令 [&& 第三条命令...]

当碰到执行出错的命令后将不执行后面的命令,如果一直没有出错则一直执行完所有命令;

|| Usage:第一条命令 || 第二条命令 [|| 第三条命令...]

当碰到执行正确的命令后将不执行后面的命令,如果没有出现正确的命令则一直执行完所有命令;

 

常用语法格式

IF [NOT] ERRORLEVEL number command para1 para2

IF [NOT] string1==string2 command para1 para2

IF [NOT] EXIST filename command para1 para2

IF EXIST filename command para1 para2

IF NOT EXIST filename command para1 para2

IF "%1"=="" goto END

IF "%1"=="net" goto NET

IF NOT "%2"=="net" goto OTHER

IF ERRORLEVEL 1 command para1 para2

IF NOT ERRORLEVEL 1 command para1 para2

FOR /L %%i IN (start,step,end) DO command [command-parameters] %%i

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do echo %i %j %k

按照字母顺序 ijklmnopq依次取参数.

eol=c - 指一个行注释字符的结尾(就一个)

skip=n - 指在文件开始时忽略的行数。

delims=xxx - 指分隔符集。这个替换了空格和跳格键的默认分隔符集。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325978973&siteId=291194637