我有一个批处理文件,它接收一个包含多行注释(堆栈跟踪)的输入参数,我想向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语句之前用空格替换它们?
答案 0 :(得分:1)
你可能运气不好:虽然可以创建多行变量,但似乎无法通过批处理文件中的参数接收它们。
如果是批量调用,您可以将多行值存储在环境变量中并传递变量的名称。
如果您以任何方式控制调用应用程序,您可以考虑更换那里的新闻行,或者可能更好,让它调用除批处理文件以外的其他内容。
另请参阅答案中包含的this SO answer和link,其中详细讨论了接收多行参数的问题。
顺便说一下,替换换行符的代码运行正常。
答案 1 :(得分: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