批处理命令 for 详解【转】

FOR loops

Basic syntax:

FOR %A IN (list) DO command [ parameters ]

 

 

listis a list of any elements, separated by either spaces, comma's or semicolons.
command

can be any internal or external command, batch file or even - in OS/2 and NT -

a list of commands

parameterscontains the command line parameters for command.
In this example, command will be executed once for every element in list, using parameters if specified.

A special type of parameter (or even command) is %A, which will be substituded by each element

 from list consecutively.

 

Full syntax

 

MS-DOS and PC-DOS
(incl. Win95's MS-DOS 7.*)  

as specified under basic syntax, plus VFAT/FAT32 long file name handling with LFNFOR in

MS-DOS 7.*

OS/2 Warp

as specified under basic syntax, though unlike in DOS, you may use redirection and 

conditional execution in the command string specified after DO

Windows NT 4/2000/XPas specified under basic syntax, plus many new options

 

Notes

 

1.%A vs. %%A
  %A is for use on command lines only.
In all examples and syntax lines shown %A should be substituted with %%A when used in batch files.
2.%a vs. %A
 

The A in %A may be replaced by any character, either upper case or lower case, except numbers.

Note, however, that variables ar case sensitive, so be consistent:
FOR %A IN (1 2 3) DO ECHO %a
will not work.
FOR %? IN (1 2 3) DO ECHO %?
on the other hand, will.

3.Nesting FOR commands
 In DOS (COMMAND.COM), nesting FOR commands is not possible.
However, by using a second command processor you may still be able to nest them:
FOR %A IN (1 2 3) DO COMMAND /C FOR %B IN (A B C) DO ECHO %A%B
Note that you cannot replace COMMAND /C with CALL

In NT (CMD.EXE) nesting is possible.
Note that to nest FOR loops, each loop requires its own variable; i.e.
FOR %A IN (1 2) DO FOR %A IN (A B) DO ECHO %A
will lead to undesired results:
A
B
A
B


FOR %A IN (1 2) DO FOR %B IN (A B) DO ECHO %A%B
will work as planned, and display:
1A
1B
2A
2B
4.Commands in list
 

list may not only contain a list of parameters, it may even contain a list of commands that

can be executed consecitively with the same parameters. a.k.a. command may also be %A:
DIR > tempfile.txt
FOR %A IN (TYPE DEL) DO %A tempfile.txt

This FOR loop will first type the temporary file and then delete it.

5. list delimiters
 

The "elements" in list can be delimited (separated) by spaces, tabs, commas or semicolons.
As of MS-DOS 7, doublequoted strings are treated as a single element, wether they contain

delimiters or not.

In NT, it is possible to define your own delimiters with FOR /F.

6.Forward slashes in list
 

If list starts with a forward slash, COMMAND.COM (except MS-DOS 7) will split the string in 

list into its first character following the forward slash and the remainder of the string.
This is demonstrated in the interactive FOR examples

7.Conditional manipulation of variables in command
 

The following does not work:
FOR %%A IN (1 2 3) DO IF "%VAR%"=="" SET VAR=%%A
sets VAR to 3, not 1
The %VAR% in the comparison (IF "%VAR%"=="") is interpreted immediately, and thus empty,

and then the FOR loop is started.
From the FOR loop's point of view, the command issued was:
FOR %%A IN (1 2 3) DO IF ""=="" SET VAR=%%A
The following construction can be used as a workaround:
FOR %%A IN (1 2 3) DO CALL TEST2.BAT %%A
If TEST2.BAT looks like this:
IF "%VAR%"=="" SET VAR=%1
then VAR is set to 1
In NT a CALL to a subroutine could be used instead.

8.Jump using GOTO in command
  FOR %%A IN (1 2 3) DO GOTO=%%A
:1
ECHO 1
GOTO End
:2
ECHO 2
GOTO End
:3
ECHO 3
:End

will display different results for different command interpreters:
  • CMD.EXE (NT and OS/2) will jump to label 1
  • COMMAND.COM (DOS) will jump to label 1, then 2, then 3, without executing any of the
  • following commands, and then execute the ECHO 3 and following commands

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值