【问题标题】:How to escape in windows command shell?如何在 Windows 命令 shell 中转义?
【发布时间】:2012-04-19 02:01:54
【问题描述】:

我在c:\Program Files 目录中有一个名为tmp.txt 的文件

对于 tmp.txt 中的每一行,我想执行一个命令。

我正在尝试使用命令提示符for 循环,但它无法找到 tmp.txt。请注意,我必须在 c:\Program Files 目录之外运行此命令。

这是我的尝试:

C:\>for /F %i in ("c:\Program Files\tmp.txt") do echo "%i"

输出是:

C:\>echo "c:\Program"
"c:\Program"

这意味着for 将"c:\Program" 视为参数并将其传递给do

如果我将文件放入c:\,然后运行for 循环作为-

C:\>for /F %i in (c:\tmp.txt) do echo "%i"

效果很好

所以我的问题是 - 如何将完整路径传递给 for 循环,以便 for 将其视为文件

【问题讨论】:

    标签: windows for-loop escaping command-line-arguments command-prompt


    【解决方案1】:

    将usebackq 选项用于for /f:

    for /f "usebackq" %i in (...)
    

    这会改变各种引号字符的语义,正如帮助中所述:

    usebackq        - specifies that the new semantics are in force,
                      where a back quoted string is executed as a
                      command and a single quoted string is a
                      literal string command and allows the use of
                      double quotes to quote file names in
                      file-set.
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2018-10-14
      • 2013-02-04
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多