`

批处理命令 for 详解【转】

阅读更多

FOR loops

Basic syntax:

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


 

list is 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

parameters contains 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/XP as 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

 

 

 

 

 

分享到:
评论

相关推荐

    批处理for命令详解

    批处理for命令详解 批处理for命令详解 批处理for命令详解

    批处理for命令详解(fly上传)

    批处理for命令详解 一前言 二for 语句的基本用法 三for /f 含变量延迟 四for /r 五for /d 六for /l

    批处理基础 FOR命令详解 批处理中的变量 set命令详解 if命令讲解 DOS编程高级技巧

    批处理基础 FOR命令详解 批处理中的变量 set命令详解 if命令讲解 DOS编程高级技巧

    批处理for命令详解(很好的一篇文章)

    FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能! 看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行只需要一个%号) FOR 参数 %%变量名 IN (相关文件或命令) DO 执行的命令

    dos批处理脚本命令详解

    对dos的批处理命令进行详细解析,应用举例,应用技巧

    bat批处理常用命令详解

    pdf文档,介绍了bat常用命令的使用! 从最基础的echo、dir到goto、for、if等各种命令! 可作为参考文档,时常查阅!

    DOS批处理命令详解

    怎么编写DOS批处理文件?这里详细讲了DOS高级命令、参数的用法,IF,FOR,GOTO,CALL..... 精通DOS的高手必备

    批处理经典入门教程(附DOS命令详解)

    以前学习批处理的时候,曾经搜索过一些资源,并将其整理成学习笔记,希望与大家共勉。...附录二 For命令详解 附录三 SET命令详解 附录四 SHIFT命令详解 附录五 命令行各命令错误返回值(ErrorLevel)一览表

    DOS 批处理命令For循环命令详解

    for命令是一种对一系列对象依次循环执行同一个或多个命令的在命令行或批处理中运行的命令,结合一些Windows管理中的程序后,其处理功能强大、应用灵活方便程度令人刮目相看

    常用批处理内部命令使用详解

    本文给大家汇总介绍了17种常用批处理内部命令使用简介以及使用方法,非常的细致,有需要的小伙伴可以参考下。

    终极dos批处理for循环命令详解

    对一个或一组文件,字符串或命令结果中的每一个对象执行特定命令,达到我们想要的结果

    DOS批处理高级教程精选合编PDF格式

    批处理基础 FOR命令详解 FOR命令中的变量 批处理中的变量 SET 命令

    MSDOS批处理基础和技巧

    第2章 DOS循环:for命令详解.doc 第3章 FOR命令中的变量.doc 第4章 批处理中的变量.doc 第5章 set命令详解.doc 第6章 if命令讲解.doc 第7章 DOS编程高级技巧--结束篇.doc 附——批处理常用符号详解.doc

    DOS批处理高级教程

    DOS批处理高级教程:第二章 DOS循环for命令详解 DOS批处理高级教程:第三章 FOR命令中的变量 DOS批处理高级教程:第四章 批处理中的变量 DOS批处理高级教程:第五章 set命令详解 DOS批处理高级教程:第六章 if命令...

    批处理详细教程之初级

    批处理初级详细教程, 批处理基础、FOR命令详解、批处理中的变量、set命令详解......

    DOS批处理高级教程 第二章 DOS循环for命令详解

    批处理 bat中的for命令的作用,非常的不错,可以节省很多效率,它可以读取文章的没一行,循环执行ping命令,探索端口,学习批处理如果不学习for将是个遗憾,希望大家多看看for 命令的实际应用的例子

    bat命令入门与高级技巧详解.doc

    更复杂的情况,需要使用if、for、goto等命令控制程式的运行过程,如同C、Basic等高级语言一样。如果需要实现更复杂的应用,利用外部程式是必要的,这包括系统本身提供的外部命令和第三方提供的工具或者软件。批处理...

Global site tag (gtag.js) - Google Analytics