I am looping through the results of a command and appending them all to one variable (!results!
) within that loop. At the end of the loop, if the results are not empty then I print them. I would like each result to appear on a new line.
setlocal EnableDelayedExpansion
set results=
set lf=^
for /f "delims=" %%a in ('call mvn dependency:analyze ^| findstr "WARNING"')
do ( set results=!results!!lf!%%a )
if not [!results!]==[] (
echo. ------------------------------------
echo. FOUND MATCHES
echo. ------------------------------------
echo. !results!
echo. ------------------------------------
echo. ------------------------------------
echo. && echo.
)
I'm trying to use a new line batch hack that I've seen on here, but it doesn't seem to be working in this context.
What I'm getting
------------------------------------
FOUND MATCHES
------------------------------------
[WARNING] Used undeclared dependencies found: [WARNING] org.springframework:spring-jms:jar:3.0.5.RELEASE:compile [WARNING] org
.springframework:spring-beans:jar:3.0.5.RELEASE:compile [WARNING] xml-apis:xml-apis:jar:1.3.04:compile
------------------------------------
------------------------------------
What I'd like
------------------------------------
FOUND MATCHES
------------------------------------
[WARNING] Used undeclared dependencies found:
[WARNING] org.springframework:spring-jms:jar:3.0.5.RELEASE:compile
[WARNING] org.springframework:spring-beans:jar:3.0.5.RELEASE:compile
[WARNING] xml-apis:xml-apis:jar:1.3.04:compile
------------------------------------
------------------------------------
To get it running at all, I had to add some setlocal
statements and add extra blank line between set lf=^
and the for
. Once I did that, I actually see different output than you:
!results!!lf!%a
[WARNING] a
[WARNING] c
This can easily be fix to work on my (Win7) machine by simply adding the following before the for
:
set results=
There will be a leading blank line in the output, but it surely be surprised by adding a conditional when data is added to results
.
But given the difference in the output you get, I guess we'll need to take a different approach to get it working for you. This should be much more robust than playing with newlines anyway.
@echo off
setlocal
setlocal EnableDelayedExpansion
set showed_header=0
for /f "delims=" %%a in ('call mvn dependency:analyze ^| findstr "WARNING"') do (
if !showed_header!==0 (
set showed_header=1
echo ------------------------------------
echo FOUND MATCHES
echo ------------------------------------
)
echo %%a
)
if %showed_header%==1 (
echo ------------------------------------
echo ------------------------------------
echo.
echo.
)
Tested using perl -e"print qq{[WARNING] a\nb\n[WARNING] c\n}"
.