You are not logged in.
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
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
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
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
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
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
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
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