Batch - FOR %%a %%b

回到顶部(go to top)

总结

%%a refers to the name of the variable your for loop will write to.

Quoted from for /?:

1
2
3
4
5
6
7
8
9
10
11
FOR %variable IN (set) DO command [command-parameters]
 
  %variable  Specifies a single letter replaceable parameter.
  (set)      Specifies a set of one or more files.  Wildcards may be used.
  command    Specifies the command to carry out for each file.
  command-parameters
             Specifies parameters or switches for the specified command.
 
To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.

  

Example 中文 1:

1
2
3
4
5
6
7
8
9
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do (
     echo %%a
     echo %%b
     echo %%c
     )
date /t 的输出结果,每行取1、2、3列
第一列对应指定的 %%a ,后面的 %%b 和 %%c 是派生出来的,对应其它列
分隔符指定为 - 和"空格",注意 delims=- 后面有个"空格"
其中 tokens=1,2,3 若用 tokens=1-3 替换,效果是一样的

  

Example 中文 2:

1
2
for /f "tokens=2* delims=- " %%a in ('date /t') do echo %%b
取第2列给 %%a ,其后的列都给 %%b

  

Example 1:

 

1
2
3
for /f "tokens=1,* eol=: delims==" %%A in (%appdata%\gamelauncher\options.txt) do (
  if "%%A"=="menu" set usemenu=%%B
)

  

Above snippet would now read the file line by line and for each line would discard everything after a colon (the eol=: option), use the equals sign as a token delimiter and capture two tokens: The part before the first = and everything after it.

 

The tokens are named starting with %%A so the second one is implicitly(含蓄的) %%B (again, this is explained in help for). Now, for each line we examine the first token and look whether it's menu and if so, assign its value to the usemenu variable. 

 

Example 2:

for %%a in (A B C D E) do Echo %%a

Produces

A
B
C
D
E

Example 3:

for %%a in (A B C) do (for %%b in (1 2 3) do Echo %%a:%%b)

Produces

A:1
A:2
A:3
B:1
B:2
B:3
C:1
C:2
C:3

 

posted on   frank_cui  阅读(784)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

levels of contents
点击右上角即可分享
微信分享提示