You are not logged in.

#1 03 Nov 2016 21:04

kingtermite
Member
Registered: 28 Oct 2016
Posts: 18

Getting a Newline to echo or as part of variable string

So I've been playing around with this today. I found some ways to echo newlines within a string, but I wanted more. I want to set the newline into an environment variable so that when I echo that variable it contains the newlines as well.

So, using a solution I found on a stackoverflow link, I came up with this which works.

for /f "delims=" %%i in ('echo:') do "set LF=%%i"

echo hello%LF%%LF%goodbye

set "SepMessage=HELLO%LF%%LF%GOODBYE"
echo %SepMessage%

Output:

C:\Temp>test2

C:\Temp>for /F "delims=" %i in ('echo:') do "set LF=%i"

C:\Temp>echo hello  & echo: & echo:goodbye
hello

goodbye

C:\Temp>set "SepMessage=HELLO& echo:& echo:GOODBYE"

C:\Temp>echo HELLO  & echo: & echo:GOODBYE
HELLO

GOODBYE



However, if I just change the environment variable used from LF to anything else, it doesn't work anymore.

for /f "delims=" %%i in ('echo:') do "set myNL=%%i"

echo hello%myNL%%myNL%goodbye

set "SepMessage=HELLO%myNL%%myNL%GOODBYE"
echo %SepMessage%

Output:

C:\Temp>test2

C:\Temp>for /F "delims=" %i in ('echo:') do "set myNL=%i"

C:\Temp>echo hellogoodbye
hellogoodbye

C:\Temp>set "SepMessage=HELLOGOODBYE"

C:\Temp>echo HELLOGOODBYE
HELLOGOODBYE


Any ideas why? I can't find anything about LF being some kind of special variable or anything.

Offline

#2 04 Nov 2016 01:22

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Getting a Newline to echo or as part of variable string

Your first example doesn't work for me

This will set a newline variable:

REM Creating a Newline variable (the two blank lines are required!)
Set NLM=^
 
 
Set NL=^^^%NLM%%NLM%^%NLM%%NLM%

Offline

#3 04 Nov 2016 19:09

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Getting a Newline to echo or as part of variable string

I'm getting the same thing. Based on my tests, it looks like right before you tried the for loop, you set LF the way Simon did it, then when the for loop ran, the variable was never overwritten so it incorrectly looked like it worked.

Offline

#4 04 Nov 2016 20:17

kingtermite
Member
Registered: 28 Oct 2016
Posts: 18

Re: Getting a Newline to echo or as part of variable string

Simon Sheppard wrote:

Your first example doesn't work for me

This will set a newline variable:

REM Creating a Newline variable (the two blank lines are required!)
Set NLM=^
 
 
Set NL=^^^%NLM%%NLM%^%NLM%%NLM%

Hmmmm....not working for me. Here's what I'm seeing as output.

C:\Temp>Set NLM=

C:\Temp>Set NL=^

C:\Temp>echo hello        goodbye
hello        goodbye

C:\Temp>set "SepMessage=HELLO^    ^    Goodbye"

C:\Temp>echo HELLO        Goodbye
HELLO        Goodbye

Offline

#5 04 Nov 2016 20:44

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 362

Re: Getting a Newline to echo or as part of variable string

kingtermite,
Simon's example works for me. Most likely if you copy/pasted you have a space on each of the 2 blank lines. Remove thaose and it will work.


Windows Shell Scripting and InstallShield

Offline

#6 04 Nov 2016 21:47

kingtermite
Member
Registered: 28 Oct 2016
Posts: 18

Re: Getting a Newline to echo or as part of variable string

RG wrote:

kingtermite,
Simon's example works for me. Most likely if you copy/pasted you have a space on each of the 2 blank lines. Remove thaose and it will work.

You were exactly right in that happened. It 'almost' works now, but not quite.

Here is the script:

@echo on

Set NLM=^


Set NL=^^^%NLM%%NLM%^%NLM%%NLM%


echo hello%NL%%NL%goodbye
set "SepMessage=HELLO%NL%%NL%Goodbye"
echo SepMessage comes next
echo %SepMessage%

It gets hung up when trying to echo %SepMessage%. It just sits there seemingly doing nothing.

Here is the output (echo turned on to see details):

C:\Temp>test2

C:\Temp>Set NLM=


C:\Temp>Set NL=^



C:\Temp>echo hello

goodbye
hello

goodbye

C:\Temp>set "SepMessage=HELLO^

C:\Temp>echo SepMessage comes next
SepMessage comes next

Offline

#7 05 Nov 2016 11:31

jeb
Member
From: Germany
Registered: 19 Nov 2010
Posts: 109

Re: Getting a Newline to echo or as part of variable string

But using linefeeds with delayed expansion is much simpler and more stable.

setlocal EnableDelayedExpansion
(set LF=^
%=EMPTY LINE=%
)

echo Line1!lf!line2
set "var=Line3!LF!Line4"
echo !var!

Percent expansion works with linefeeds too, but it's more complex to handle and it doesn't work in quotes.

Offline

#8 07 Nov 2016 18:34

kingtermite
Member
Registered: 28 Oct 2016
Posts: 18

Re: Getting a Newline to echo or as part of variable string

jeb wrote:

But using linefeeds with delayed expansion is much simpler and more stable.

setlocal EnableDelayedExpansion
(set LF=^
%=EMPTY LINE=%
)

echo Line1!lf!line2
set "var=Line3!LF!Line4"
echo !var!

Percent expansion works with linefeeds too, but it's more complex to handle and it doesn't work in quotes.

Thanks Jeb. That seemed to finally work the way I wanted it to. I was purposely trying not to use setlocal as, for my purpose, I don't want that on. However, I see that I can set that variable and then just scope the setlocal to the area I need to set/echo with a line feed.

Offline

Board footer

Powered by