像我儿子一样永远保持好奇和梦想

【编撰】BAT文件的写法 -2

上一篇 / 下一篇  2012-03-15 11:35:08 / 个人分类:linux

[hsy75] 如何查询这些命令的帮助,最好的方式就是如下:ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台5[nc3B%VR
直接在dos command 窗户输入:
3w^r*{`S`.j0for /? >for.txt  
Id"fg1t0在for.txt 中你就会得到帮助了:ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台E?2lEFw'@ _-k

【hsy75案 以下是直接从HELP文件中拷贝出来的例子和解释,这里稍微做了点分析】
[$?`w#F,g;W|l0helps for for command:ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台'P9}!mpr+xZ!h
如下:
{$X w8N|+}0w j0Runs a specified command for each file in a set of files.
(A{/\ @6Y&{'rdC8r0
!_Z.vLGjN2t$r*tp'u0FOR %variable IN (set) DO command [command-parameters]
CI0q:Ol"g0ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台?tDN/?,U-xf
  %variable  Specifies a single letter replaceable parameter.
&z?g,Hck-r[0  (set)      Specifies a set of one or more files.  Wildcards may be used.
,d S4_4v&s0  command    Specifies the command to carry out for each file.
3NX6Z0I^8X0  command-parameters
/?GT8l$w k%u@!Q8a0             Specifies parameters or switches for the specified command.
$P'df#g'd3xA0
4\6v \CN'}@0To use the FOR command in a batch program, specify %%variable insteadET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台NY-fVsf4?a
of %variable.  Variable names are case sensitive, so %i is different
$A3mm b#p(C6O$h3}0from %I.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台$j:Q a9C;t^,O

R$^:n NTR2yP0If Command Extensions are enabled, the following additionalET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台Q5c mamT@2D8Xy$X{&m
forms of the FOR command are supported:
hQ'kAmt0ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台k6j2Xl LO
FOR /D %variable IN (set) DO command [command-parameters]ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台A ? F'C!W5n6g7y

:{+v I(U5[ d3]+nL4m0    If set contains wildcards, then specifies to match against directory
DpM9G(@'v a)s0    names instead of file names.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台-AqX`s;C]N3lJ
ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台sA#}x:b
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
J UFrsrx0ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台 tTI s&M:yi
    Walks the directory tree rooted at [drive:]path, executing the FORET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台/i~5G#KE f"R
    statement in each directory of the tree.  If no directoryET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台7{7|7G4Md!M5_?
    specification is specified after /R then the current directory is
$wAL kp6v4Y&GK {l0    assumed.  If set is just a single period (.) character then itET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台)l%c8N_-PT.ov
    will just enumerate the directory tree.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台'Z'h9F3pk2C
ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台}OEb%A{0aZff\
FOR /L %variable IN (start,step,end) DO command [command-parameters]ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台L y-X YBx

3voh`'u)J Ay0    The set is a sequence of numbers from start to end, by step amount.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台m0^1TTF
    So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) wouldET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台1y xT~P{H,d
    generate the sequence (5 4 3 2 1)
4E k{B8KP;jd0
*pQ7m4N&frU oZ0FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台_a|M$sl C&D [*U)E1B
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
GwR0~a5eg[0FOR /F ["options"] %variable IN ('command') DO command [command-parameters]ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台%Y1|5G-?|%I/g
ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台ccZdC vi
    or, if usebackq option present:ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台.v RWE'Z u
ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台Dm9okoY5Re
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台$Rm2j5Y8Az
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台w&AFitpx \
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台;C#F a9F oy
ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台`gL:t5HmL)j,b
    filenameset is one or more file names.  Each file is opened, read
GeHN3X}2?6?0    and processed before going on to the next file in filenameset.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台#K'[4GYl.\9E&`3J {
    Processing consists of reading in the file, breaking it up into
_Jq7r@5k6ea C.\0    individual lines of text and then parsing each line into zero or
8tL#F l2du&}#nOcP0    more tokens.  The body of the for loop is then called with the
P.SK5j(M9]0    variable value(s) set to the found token string(s).  By default, /FET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台QVd/H&W'OP5i`
    passes the first blank separated token from each line of each file.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台 X:v8I T*^
    Blank lines are skipped.  You can override the default parsing
0x%f)ZG2I0    behavior. by specifying the optional "options" parameter.  ThisET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台/Gf-@sb a#|
    is a quoted string which contains one or more keywords to specify
y3}1a`:CS0    different parsing options.  The keywords are:
?2] LM/df[X0
$L!k acmy9m4U6T0        eol=c           - specifies an end of line comment character
6n#c*k5S V V;L2d0                          (just one)
L`%i.a5r0        skip=n          - specifies the number of lines to skip at theET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台 P,K'P R6\|1o
                          beginning of the file.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台'z\@$xqfRE
        delims=xxx      - specifies a delimiter set.  This replaces theET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台9MGb'iw1G _$S
                          default delimiter set of space and tab.
re%z1a"U!ZX8M0  【hsy75案】delims 设置了出空格和TAB以外的参数限制分割符号,这个非常有用,当从配置文件尤其是linux的文件配置里面拿出配置的参数应用于当前的bat文件的时候
~ b3dVYneqF0                         往往会遇到各种分割符号定义,比如( , ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台)]T0N"@-b JcL!q)`V
        tokens=x,y,m-n  - specifies which tokens【代换】 from each line are toET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台/D*Dhs.st
                          be passed to the for body for each iteration.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台c \ tM2~$D h ZT
                          This will cause additional variable names to
,rpbL;eC9w e7ck0                          be allocated.  The m-n form. is a range,
~\T h!QH0                          specifying the mth through the nth tokens.  If
s lB!f\e cZ0                          the last character in the tokens= string is an
-vO5|!K$]0                          asterisk【星号】, then an additional variable isET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台9c5D9ZHd#a-?O
                          allocated and receives the remaining text on
7n%G@z!Xy0                          the line after the last token parsed.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台4T9WB4O2hT
        usebackq        - specifies that the new semantics are in force,ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台W$aN@+r9O
                          where a back quoted string is executed as aET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台TE I;f.V:N
                          command and a single quoted string is a
x*y,lHx Qz0                          literal string command and allows the use ofET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台Y n&oXl ]u
                          double quotes to quote file names in
UB'} \(F1X Y0                          filenameset.
^R;nu5a a0
(z'B(_h |@!E Q0    Some examples might help:ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台`)U4K1n;U8y9M#d6L
ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台 ]#\^ \Q*?{M*M2s/Qvp
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %kET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台K6Uh4^ px,L ^fI)V
ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台C1{]!LU`$M+q2H
    would parse 【解析】each line in myfile.txt, ignoring lines that begin with
~;bu/o8w1o9jE0    a semicolon【分号】, passing the 2nd and 3rd token from each line to the for
(xp bB l x](lT0    body, with tokens delimited by commas and/or spaces【hsy75案,这里逗号分号都是分割符号了】.  Notice the for
G)Km p,x$QBho.HD0    body statements reference %i to get the 2nd token, %j to get the
${ l&S:Ew0    3rd token, and %k to get all remaining tokens after the 3rd【hsy75案,这里表示的bat的临时替换变量,依次按顺序获取文件分割符号分割好的字符参数】. ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台 SW DN3~@x
    For file names that contain spaces, you need to quote the filenames withET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台)L ^%Y,l6z W'm(?,d] D"[
    double quotes【hsy75案,因为空格是默认的分割符,所以如果你的字符参数含有空格,就必须用双引号括起来】.  In order to use double quotes in this manner, you alsoET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台VnvE0X4d
    need to use the usebackq option, otherwise the double quotes will beET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台G4RJ}I&raI
    interpreted as defining a literal string to parse.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台6]G*s_9zG6@:L

py1Y NGc%B4j)p+`0    %i is explicitly declared in the for statement and the %j and %k
0}-O p9o6dC0    are implicitly declared via the tokens= option.  You can specify up
.ec'm;h x,['k:U0    to 26 tokens via the tokens= line, provided it does not cause anET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台A+eYcm+y*N5|*x/N0H
    attempt to declare a variable higher than the letter 'z' or 'Z'.
fv)Jk"x|0    Remember, FOR variables are single-letter, case sensitive, global,
*noM:\/O Nr0    and you can't have more than 52 total active at any one time.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台`1Yl&wO

D)x6P PHu;k2H'y0    You can also use the FOR /F parsing logic on an immediate string, byET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台-f$x0SF@*qz@
    making the filenameset between the parenthesis a quoted string,ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台_R0Mv-d
    using single quote characters.  It will be treated as a single line
`"O rq j%@g*V0    of input from a file and parsed.
dr&S*bpT0
SFbrmX F rym0    Finally, you can use the FOR /F command to parse the output of a
d?4L"oz.tWv0    command.  You do this by making the filenameset between theET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台 c G&E!Bg7ys9ec0iL
    parenthesis a back quoted string.  It will be treated as a command
yo'V2y0{HV0\0    line, which is passed to a child CMD.EXE and the output is captured
\C:F)g#d0    into memory and parsed as if it was a file.  So the followingET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台V3~ [A zW]7c
    example:
`{Mr C*j0ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台 p)f%AaA)^
      FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
e3{a'J.u"hwz0
m7eD nmv0    would enumerate the environment variable names in the currentET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台jh*]2`CK:CX
    environment.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台%c2H9i1@9d+wv

j,Zsd9p'_0In addition, substitution of FOR variable references has been enhanced.ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台Xy K-|x$lt;xhJ
You can now use the following optional syntax:
J!_LQ p8@3^}C0
H$LW:p? _qz(v0    %~I         - expands %I removing any surrounding quotes (")ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台l(KX H{U_E;B%R/x1W
    %~fI        - expands %I to a fully qualified path nameET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台v4T,R1HM$PJ W+ML
    %~dI        - expands %I to a drive letter onlyET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台5etTi E,U"b,_
    %~pI        - expands %I to a path only
h*H0I \.P;J-\c'x1^0    %~nI        - expands %I to a file name onlyET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台*O0S5r KE'A9M$gp
    %~xI        - expands %I to a file extension only
Z"w D7m/M/P2f+~E0    %~sI        - expanded path contains short names onlyET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台cUn$Sa"[)d
    %~aI        - expands %I to file attributes of file
v'l%@ O![9Y0    %~tI        - expands %I to date/time of file
8X9J@W J(r4`U0    %~zI        - expands %I to size of file
.v(Oo @N2Bs5L0    %~$PATH:I   - searches the directories listed in the PATH
Z%n7Sa.k4X1F0           &nET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台0C*O3{1}k

ref :http://www.jb51.net/article/7131_5.htm
%pE7V&\4wh-C0
tb2gARL+r0
/{?Ka8X i0K0
a$fl;t-n}6K0ET创芯网(EETOP)-电子设计论坛、博客、超人气的电子工程师资料分享平台+z7v2@KD.|?o2W2i0q

9xdy$CA0

TAG: BAT command file specified

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar