【问题标题】:echoing a command to an output file from within a batch file [duplicate]从批处理文件中将命令回显到输出文件[重复]
【发布时间】:2018-03-13 22:13:34
【问题描述】:
echo ((goto) 2>nul & cd "%~dp0\.." && rmdir /s /q "%~dp0") >> delete.bat

我正在使用批处理文件通过运行此命令来创建另一个单独的批处理文件。但是,上面的行根本没有出现在“delete.bat”文件中。我确定这是语法问题,但我无法看到问题到底是什么。有人知道吗?

【问题讨论】:

  • 双&符号必须被转义。
  • 我质疑你为什么需要这样做。为什么你不能编写一个脚本文件并使用适当的参数调用它?如果是清理脚本,则编写一个数据文件供其读取。较低的复杂性总是好的。
  • ...以及脱字符号转义的 & 符号,所有百分比字符也需要加倍!

标签: windows batch-file cmd


【解决方案1】:
> echo test ^&^& test
test && test
> echo test && testing
test
'testing' is not recognized as an internal or external command,
operable program or batch file.

由于&&,Cmd.exe 将您的echo 行解释为两个命令。您需要使用 ^ 帽子将它们转义以将它们发送到脚本中。

Quoting and escaping

【讨论】:

  • 不错!这解决了最初的问题,但另一个问题出现了。我将如何转义变量以便后续批处理文件中包含%~dp0
  • 哈哈!试试%%~dp0。见Quoting and escaping
  • 涂料,这解决了它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
  • 1970-01-01
  • 2012-07-14
  • 2019-08-09
  • 2019-11-15
  • 2012-05-28
相关资源
最近更新 更多