1st off the caret (^
) is the escape character for cmd, when used as the last character on a line it removed the end of the line and appends the next line and escapes the 1st character
2ndly contrary to popular belief, you can write multi-line commands into the cmd prompt, normally this is done by wrapping the commands in parenthesis.
eg:
c:\>@(
more? echo.hello
more? echo.there
more? )
---[output]---
hello
there
An alternative way is to do this is to use the caret at the end of the line.
When the last character on the 1st line is a caret, it means the end of line is not processed (technically its removed along with the caret), and the first character of the 2nd line will be escaped.
So, on the 2nd line, if you just hit "enter" (carriage return) it will be escaped, and then you get the 2nd more?
because that line will now also.be appended to the 1st.
however you've only escaped that 2nd EOL, Which means this is a method to save a carriage return into a variable. However echoing the line using the variable only works if delayed expansion is enabled.
So For Example This at the CLI is just fine:
c:\>echo.this ^
more? and that
---[result]---
this and that
You can also use this for setting Variables at the CLI and not have the carriage return matter
c:\>Set var=Some ^
more? Text
c:\> echo %var%
---[result]---
Some Text
As mentioned if echoing, and ^ is the last character of the line and you press enter on the first more?
you will still be able to write more to the line and have it show up including the carriage return. This works without delayed expansion.
c:\>echo.this .^
more?
more? and that
---[result]---
this .
and that
however while you can save a variable on the CLI as mentioned above the following behavior will show up when you try to echo
it:
c:\>Set var=Some ^
more?
more? Text
c:\> echo %var%
---[result]---
Some
This is because saving the variable this way will work even without delayed expansion, however to echo
the value through the EOL character you will need to have delayed expansion on.
However we Can see that it is stored with the carriage return using SET
c:\>Set var=Some ^
more?
more? Text
c:\> SET var
---[result]---
var=Some
Text
So you can still get it out with a FOR
loop, or you will need to turn on delayed expansion, this can be done by starting CMD
with the /V
option.
So this simple for
loop will work, but only when there are no spaces or commas in the string stored in the variable.
So this will work:
c:\Set var=This_Has_no...Spaces^
More?
More? In_it
c:\>for %A IN (%var%) DO @(ECHO.%~A)
---[result]---
This_Has_no...Spaces
In_it
But this isn't going to get what you want:
c:\>Set var=Some more text ^
more?
more? goes here
c:\>for %A IN (%var%) DO @(ECHO.%~A)
---[result]---
Some
more
text
goes
here
However, as mentioned, SET
does show the carriage return so you could get both lines out using a FOR /F
Loop:
c:\>Set var=Some more text ^
more?
more? goes here
c:\>for /F "Tokens=*" %A IN ('SET VAR') DO @(ECHO.%~A)
---[result]---
var=Some more text
goes here
As but now we still have var=
at the beginning so we'd really prefer not to have that which means we need an additional carriage return at the beginning of the variable and to skip the 1st line, to get it like this:
c:\>Set var=^
More?
More? Some text here ^
More?
More? Also Here
c:\>for /F "Tokens=* SKIP=1" %A IN ('SET VAR') DO @(ECHO.%~A)
---[result]---
Some text here
Also Here
So the easiest way to accomplish this is instead to enable delayed expansion and use echo as you normally would:
c:\>Set var=Some ^
more?
more? Text
c:\>cmd /V
c:\> echo !var!
---[result]---
Some
Text