Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 是用“的;代币=*";或;delims=";功能等效?_Windows_Batch File_For Loop_Cmd - Fatal编程技术网

Windows 是用“的;代币=*";或;delims=";功能等效?

Windows 是用“的;代币=*";或;delims=";功能等效?,windows,batch-file,for-loop,cmd,Windows,Batch File,For Loop,Cmd,考虑以下两个循环: for /f "tokens=*" %%a in ('dir /b %TEMP%') do ( echo %%a ) 及 如果我想要的结果是为变量%%a提供整行被评估的内容,那么选项tokens=*和delims=在功能上是否等效 在任何情况下,如果给定一个特定的输入,我是否会得到一个不同的输出 我想知道这两个选项是否应始终结合使用,以确保覆盖所有潜在案例,或者指定这两个选项是否是多余的。是的,标记=*和delims=是不同的: delims=返回未编辑的整行 to

考虑以下两个循环:

for /f "tokens=*" %%a in ('dir /b %TEMP%') do (
   echo %%a
)

如果我想要的结果是为变量
%%a
提供整行被评估的内容,那么选项
tokens=*
delims=
在功能上是否等效

在任何情况下,如果给定一个特定的输入,我是否会得到一个不同的输出


我想知道这两个选项是否应始终结合使用,以确保覆盖所有潜在案例,或者指定这两个选项是否是多余的。

是的,
标记=*
delims=
是不同的:

  • delims=
    返回未编辑的整行
  • tokens=*
    返回删除任何前导分隔符(默认为空格和制表符)的行
  • tokens=*delims=
    的行为方式与
    delims=
    完全相同
请注意,始终跳过空行。也包括以
开头的行被忽略,因为该字符是默认的
eol

如果指定了
tokens=*
,并且一行仅包含分隔符,则/F
循环将迭代
for,元变量返回空字符串。一旦提供任何令牌编号(如
tokens=3
tokens=1,3
tokens=2-3
tokens=2*
等),只跳过分隔符行。
但是,即使提供了
tokens=*
,包含一个或多个分隔符加上
eol
字符加上任意字符串的行也会被忽略


为了证明我做了一些测试,我使用了以下文本文件
sample.txt
(注意,第2行是空的,第4行包含四个空格;单击此答案下方的按钮并查看原始文本):

下面是我在控制台上所做的操作以及相应的返回字符串:

>>> for /F %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text"
"text"
"text"

>>> for /F "tokens=*" %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text with delimiters"
""
"text with leading and trailing delimiters  "
"text plus ; comment text"

>>> for /F "delims=" %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text with delimiters"
"    "
"  text with leading and trailing delimiters  "
"  ; comment text with leading delimiters"
"text plus ; comment text"

>>> for /F "tokens=* delims=" %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text with delimiters"
"    "
"  text with leading and trailing delimiters  "
"  ; comment text with leading delimiters"
"text plus ; comment text"

>>> for /F "tokens=3" %I in (sample.txt) do @echo "%I"
"delimiters"
"leading"
";"

>>> for /F "tokens=1,3" %I in (sample.txt) do @echo "%I"    "%J"
"text_without_delimiters"    ""
"text"    "delimiters"
"text"    "leading"
"text"    ";"

>>> for /F "tokens=2-3" %I in (sample.txt) do @echo "%I"    "%J"
"with"    "delimiters"
"with"    "leading"
"plus"    ";"

>>> for /F "tokens=2*" %I in (sample.txt) do @echo "%I"    "%J"
"with"    "delimiters"
"with"    "leading and trailing delimiters  "
"plus"    "; comment text"

因此,唯一真正奇怪和出乎意料的输出是带有
标记=*
选项的行

如果我们发布了OP没有要求的荒谬想法,你最好学习Python或C。说出一种没有任何怪癖的编程语言。
“delims=“
表示“整行”
“tokens=*”
表示“所有的令牌”,但第一个令牌之前的空格不包括在本例中,因此当该行有前导空格时,结果会有所不同。@Bill\u Stewart:如果您在批处理文件问题中发布未请求的PowerShell注释(对批处理有一些批评),然后我有权对PowerShell发表反批评。我邀请您阅读由批处理文件用户撰写的关于他们在PowerShell上的体验的评论,或由经验丰富的程序员撰写的有趣的文章。您可以阅读我关于批处理文件问题的非请求PS解决方案的观点;例如:“是的:PowerShell比批处理文件更重要(在这里放上您想要的任何好的形容词),但这并不意味着它在所有情况下都是正确的选择,特别是当要解决的问题很简单时,比如这一个”。
text_without_delimiters

text with delimiters

  text with leading and trailing delimiters  
; comment text
  ; comment text with leading delimiters
text plus ; comment text
>>> for /F %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text"
"text"
"text"

>>> for /F "tokens=*" %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text with delimiters"
""
"text with leading and trailing delimiters  "
"text plus ; comment text"

>>> for /F "delims=" %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text with delimiters"
"    "
"  text with leading and trailing delimiters  "
"  ; comment text with leading delimiters"
"text plus ; comment text"

>>> for /F "tokens=* delims=" %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text with delimiters"
"    "
"  text with leading and trailing delimiters  "
"  ; comment text with leading delimiters"
"text plus ; comment text"

>>> for /F "tokens=3" %I in (sample.txt) do @echo "%I"
"delimiters"
"leading"
";"

>>> for /F "tokens=1,3" %I in (sample.txt) do @echo "%I"    "%J"
"text_without_delimiters"    ""
"text"    "delimiters"
"text"    "leading"
"text"    ";"

>>> for /F "tokens=2-3" %I in (sample.txt) do @echo "%I"    "%J"
"with"    "delimiters"
"with"    "leading"
"plus"    ";"

>>> for /F "tokens=2*" %I in (sample.txt) do @echo "%I"    "%J"
"with"    "delimiters"
"with"    "leading and trailing delimiters  "
"plus"    "; comment text"