1

我有一个批处理文件,它接收包含我想报告给 TeamCity 的多行注释(堆栈跟踪)的输入参数。下面的代码适用于单行注释并从消息中删除括号和引号。问题是只报告多行消息的第一行。

rem Try to remove line breaks (not working)
setlocal EnableDelayedExpansion
SET message=%~1
set ^"message=!message:^

= !"

rem Remove square brackets and quotes
SET message=%message:[=%
SET message=%message:]=%
SET message=%message:'=%

rem Print message to console for TeamCity
echo ##teamcity[progressMessage '%message%']

有没有办法删除所有换行符并在 echo 语句之前用空格替换它们?

4

2 回答 2

1

您可能不走运:虽然可以创建多行变量,但似乎不可能通过批处理文件中的参数接收它们。

如果是批处理调用,您可以将多行值存储在环境变量中并传递变量的名称。

如果您以任何方式控制调用应用程序,您可以考虑替换那里的新闻行,或者可能更好,让它调用批处理文件以外的其他东西。

另请参阅此 SO 答案和答案中包含的链接,该链接更详细地讨论了接收多行参数的问题。

顺便说一句,您替换换行符的代码工作正常。

于 2012-09-17T12:32:50.550 回答
1

我想没有通用的解决方案。
但它可能为您提供解决方案!

这取决于调用批处理的方式,如果通过新的 cmd 实例调用它,则可以使用 !cmdcmdline! 即使使用换行符和回车符也可以访问完整的参数。

@echo off
setlocal EnableDelayedExpansion
set "message=!cmdcmdline!"
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
set LF=^


REM ** Two empty lines are required
FOR %%L in ("!LF!") do set "message=!message:%%~L=\n!"
FOR %%C in ("!CR!") do set "message=!message:%%~C=\r!"
echo ##teamcity[progressMessage '!message!']

您可以使用 invoke.bat 对其进行测试

@echo off
setlocal EnableDelayedExpansion
set LF=^


for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
cmd /c test one!lf!two!CR!three
于 2012-09-17T17:24:59.713 回答