波浪线或不波浪线

Vom*_*yle 5 batch-file cmd.exe

在编写批处理FOR循环时,我养成了tildes在脚本命令部分的变量占位符名称中使用的习惯,并用双引号将其括起来。

示例语法

FOR %%A IN ("*.*") DO ECHO "%%~A"
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

原因

我通常会不假思索地编写这样的脚本作为标准,无论它们是否真的需要。我喜欢它如何从set循环的部分中去除双引号,然后在循环的command parameter部分中显式地将它们添加回来。

  • FOR通过批处理脚本或命令行与循环一起使用时,是否存在使用波浪号实际上有害的情况?

  • 其他人的经验或可能在某处正式记录的原因是什么,为什么您会或不想tildes为此原因FOR通过 cmd 或批处理使用循环?

Lot*_*ngs 3

在您的确切用例中,它不会产生任何影响,但也不会造成损害:

  • 因为通配符迭代的文件名总是不带引号返回。

当迭代一些双引号字符串(特别是 cmd line args %*)时
,我总是使用它们:

@Echo off
For %%A IN (
    "this is a sentence"
    two words
    "some words"
) Do (
    Echo with tilde: "%%~A"
    Echo w/o  tilde: "%%A"
    Echo:
)
Run Code Online (Sandbox Code Playgroud)
> SU1397704.cmd
with tilde: "this is a sentence"
w/o  tilde: ""this is a sentence""

with tilde: "two"
w/o  tilde: "two"

with tilde: "words"
w/o  tilde: "words"

with tilde: "some words"
w/o  tilde: ""some words""
Run Code Online (Sandbox Code Playgroud)