FOR语句,循环控制,可以解析文件,大杀器。
基本语法
1 | for {%% | %}variable in (set) do command [ CommandLineOptions] |
其中,%variable
或%%variable
必须,%variable
用来表示从命令提示符传递过来的参数,%%variable
表示bat脚本自身的参数,参数大小写敏感。(set)
必须,指定一个或多个文件、文件夹、一定范围的数值、字符串,可使用通配符,括号不可少。command
必须,指定对(set)
集合中每一个元素执行的命令。CommandLineOptions
可选,主要有/D
-目录,/R
-递归目录,/L
-数值,/F
-过滤。
无参数
for %%f in (*.doc *.txt) do type %%f
当前目录中的.doc或者.txt文件,依次代替%%f
的位置,然后作为type
命令的参数执行。
for %%f in (*.txt) do @echo %%f
效果类似于dir /B *.txt
。
参数/D 目录
1 | FOR /D {%% | %}variable IN (set) DO command [command-parameters] |
只能匹配当前目录,不匹配任何文件,也不会匹配下一级目录。for %%f in (D:\*) do @echo %%f
- D盘下所有的文件for /D %%f in (D:\*) do @echo %%f
- D盘下所有的目录for /D %%i in (???) do echo %%i
- 显示当前目录下名字只有1-3个字母的目录
参数/R 递归目录
1 | FOR /R [[drive:]path] {%% | %}variable IN (set) DO command [command-parameters] |
此命令会搜索指定路径[[drive:]path]
及所有子目录中与set相符合的所有文件,注意是指定路径及所有子目录。
- set中的文件名如果含有通配符(?或*),则列举/R参数指定的目录及其下面的所用子目录中与set相符合的所有文件,无相符文件的目录则不列举。
- 如果set中为具体文件名,不含通配符,则枚举该目录树(即列举该目录及其下面的所有子目录),并在每个目录后面加上具体的文件名,而不管set中的指定文件是否存在。
for /r c:\ %%i in (*.exe) do echo %%i
- 把C盘根目录,和每个目录的子目录下面全部的EXE文件都列出来了for /r c:\ %%i in (boot.ini) do echo %%i
- 枚举了c盘所有目录,并在目录后面加上boot.ini,不管是否存在该文件for /r c:\ %%i in (boot.ini) do if exist %%i echo %%i
- 很好的搜索命令,列举boot.ini存在的目录
参数/L 数列
1 | FOR /L {%% | %}ariable IN (start#,step#,end#) DO command [command-parameters] |
该集表示以增量形式从开始到结束的一个数字序列。因此,(1,1,5)将产生序列1 2 3 4 5,(5,-1,1)将产生序列(5 4 3 2 1).for /l %%i in (1,2,10) do @echo %%i
- 输出1 3 5 7 9for /l %%i in (100,-20,1) do @echo %%i
- 输出100,80,60,40,20for /l %%i in (1,1,5) do start cmd
- 打开5个CMD窗口for /l %%i in (1,1,5) do md %%i
- 建立从15共5个文件夹5共5个文件夹for /l %%i in (1,1,5) do rd /q %%i
- 删除从1
参数/F 迭代文件解析
Use file parsing to process command output, strings and file content. Use iterative variables to define the content or strings you want to examine and use the various ParsingKeywords options to further modify the parsing. Use the ParsingKeywords token option to specify which tokens should be passed as iterator variables. Note that when used without the token option, /F will only examine the first token.
File parsing consists of reading the output, string or file content, breaking it up into individual lines of text and then parsing each line into zero or more tokens
. The for loop is then called with the iterator variable value set to the token. By default, /F passes the first blank separated token from each line of each file. Blank lines are skipped. The different syntaxes are:
1 | for /F ["ParsingKeywords"] {%% | %}variable in (filenameset) do command [CommandLineOptions] |
The filenameset argument specifies one or more file names. Each file is opened, read and processed before going on to the next file in filenameset. To override the default parsing behavior, specify “ParsingKeywords”. This is a quoted string that contains one or more keywords to specify different parsing options.
If you use the usebackq option, use one of the following syntaxes:
1 | for /F ["usebackqParsingKeywords"] {%% | %}variable in ("filenameset") do command [CommandLineOptions] |
when using usebackq
option, be careful about the difference:filenameset
-> "filenameset"
"LiteralString"
-> 'LiteralString'
'command'
-> `command`
The following table lists the parsing keywords that you can use for ParsingKeywords.
eol=c - 指一个行注释字符的结尾(就一个)
skip=n - 指在文件开始时忽略的行数。
delims=xxx - 指分隔符集。这个替换了空格和制表符的默认分隔符集。
tokens=x,y,m-n - 指每行的哪一个符号被传递到每个迭代的 for 本身。这会导致额外变量名称的分配。m-n
格式为一个范围。通过 nth 符号指定 mth。如果符号字符串中的最后一个字符星号,那么额外的变量将在最后一个符号解析之后
分配并接受行的保留文本。
usebackq - 指定新语法已在下类情况中使用:
在作为命令执行一个后引号的字符串并且一个单引号字符为文字字符串命令并允许在 file-set
中使用双引号扩起文件名称。
如eol=;
表示忽略;
开头的行;
skip=1
表示忽略第一行;
delims=,
表示用,
或对字符串分隔;
tokens=2,3*
将每行中的第二个和第三个符号传递给 for 程序体
1 | for /F "eol=; tokens=2,3* delims=, " %%i in ("A1,A2,A3,A4,A5") do echo %%i %%j %%k |
1 | FOR /F "tokens=1* delims==" %%i IN ('set') DO @echo [%%i----%%j] |
1 | for /F "usebackq delims==" %%x in (`dir /b`) do @echo %%~fx |
Reference:
1 | 对一组文件中的每一个文件执行某个特定命令。 |