首页 > 解决方案 > 我如何必须转义此批处理代码中的引号?

问题描述

我如何必须转义此批处理代码中的引号?

我有以下代码:

echo ""powershell -Executionpolicy Bypass -Command "get-clipboard > C:\File.txt""" > C:\Helptool.cmd

此代码应生成文件“Helptool.cmd”。内容应如下所示:

powershell -Executionpolicy Bypass -Command "get-clipboard > C:\File.txt

我必须在哪里将 ^ 字符放在第一个批处理代码中?

当我运行上述代码时,新创建的批处理文件的内容错误如下:

"" powershell -Executionpolicy Bypass -Command "get-clipboard> C: \ File.txt" ""

开头的两个引号和结尾的三个引号是不可取的。我会通过逃跑摆脱他们。或者?我该怎么做呢?

标签: batch-file

解决方案


^如果您对整个 PowerShell 进行双引号,则不需要任何插入符号-Command

这个版本使用反斜杠来转义嵌套的双引号:

@Echo @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-Clipboard -Format Text -Raw -TextFormatType UnicodeText > \"C:\File.txt\"" > "C:\Helptool.cmd"

如果您更喜欢在 PowerShell 中使用单引号,则无需转义:

@Echo @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-Clipboard -Format Text -Raw -TextFormatType UnicodeText > 'C:\File.txt'" > "C:\Helptool.cmd"

请注意,我已删除您的-ExecutionPolicy参数,因为运行 s 不需要它-Command(仅 PowerShell -Files)。可执行文件的完整路径和扩展名powershell是可选的,但建议与-NoProfile参数一样。您应该打开一个 Windows PowerShell 窗口,并从 中读取输出Get-Help Get-Clipboard -Full,以了解-FormatRaw-TextFormatType参数的作用。


推荐阅读