windows 批处理文件 FOR /f 令牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6966558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:39:01  来源:igfitidea点击:

Batch file FOR /f tokens

windowsbatch-filefor-looptoken

提问by Joey

Can anyone please explain exactly how the following code works, line by line. I'm really lost. I've been trying to learn how to use the FOR command but I don't understand this.

任何人都可以请逐行解释以下代码的工作原理。我真的很失落。我一直在尝试学习如何使用 FOR 命令,但我不明白这一点。

@echo off

for /f "tokens=* delims= " %%f in (myfile) do (
  set line=%%f
  call :processToken
  )
  goto :eof

:processToken

  for /f "tokens=1* delims=/" %%a in ("%line%") do (
  echo Got one token: %%a
  set line=%%b
  )
  if not "%line%" == "" goto :processToken
  goto :eof

回答by Joey

for /f "tokens=* delims= " %%f in (myfile) do

This reads a file line-by-line, removing leading spaces (thanks, jeb).

这会逐行读取文件,删除前导空格(谢谢,jeb)。

set line=%%f

sets then the linevariable to the line just read and

然后将line变量设置为刚刚读取的行并

call :procesToken

calls a subroutine that does something with the line

调用一个对线路做某事的子程序

:processToken

is the start of the subroutine mentioned above.

是上面提到的子程序的开始。

for /f "tokens=1* delims=/" %%a in ("%line%") do

will then split the line at /, but stopping tokenization after the first token.

然后将在 处拆分该行/,但在第一个标记之后停止标记化。

echo Got one token: %%a

will output that first token and

将输出第一个令牌和

set line=%%b

will set the linevariable to the rest of the line.

line变量设置为行的其余部分。

if not "%line%" == "" goto :processToken

And if lineisn't yet empty (i.e. all tokens processed), it returns to the start, continuing with the rest of the line.

如果line还不是空的(即所有标记已处理),它返回到开始,继续该行的其余部分。