从批处理文件调用PowerShell失败

时间:2014-08-18 16:25:06

标签: windows batch-file powershell cmd

当我从命令行调用以下命令时,一切正常。但是,当我把它放在.bat文件中并调用该文件时,我得到语法错误,说我错过了关闭'在命令的最后。

powershell -command "& 'C:\Program Files\TortoiseSVN\bin\svn.exe' status  | ? { $_ -Match '^!\s+(.*)' } | % { & 'C:\Program Files\TortoiseSVN\bin\svn.exe' rm $Matches[1] }"

当我从.bat调用它时发生的奇怪事情是该命令被神秘地改变了。这是.bat文件的输出:

C:\workspace>powershell -command "& 'C:\Program Files\TortoiseSVN\bin\svn.exe' status  | ? { $
_ -Match '^!\s+(.*)' } | \Program Files\TortoiseSVN\bin\svn.exe' rm $Matches[1] }"
The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

我100%确定.bat文件的内容和我手动运行的命令是相同的,并且编辑器(Notepad ++)不执行像两行中断命令这样的愚蠢行为。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

字符串中任何位置的字符%必须在批处理文件中使用一个%进行转义,才能被解释为文字字符。

powershell -command "& 'C:\Program Files\TortoiseSVN\bin\svn.exe' status  | ? { $_ -Match '^!\s+(.*)' } | %% { & 'C:\Program Files\TortoiseSVN\bin\svn.exe' rm $Matches[1] }"

在批处理文件中工作。在命令提示符窗口help forfor /?中输入,您可以在第一个帮助页面输出中阅读有关转义%的此要求。

相关问题