Page 1 of 1

Another way to create a line feed variable

Posted: 29 Mar 2013 16:02
by carlos
Hello. I want the way of save the output of command PROMPT $_ this is a new line character.
I search this way instead use:

Code: Select all

(set nl=^

)


I search a way of not use the above code because some forums add a space character at the end of each line, and then the code will not work. I want use prompt for it, but I cannot catch it because for /f ignore CR character.

Re: I there a way for save the result of Prompt $_

Posted: 29 Mar 2013 19:20
by Squashman
As far as I know from being on this forum, the only way to get a carriage return and line feed into a variable is to do this.

Code: Select all

::Define LF variable containing a linefeed (0x0A)
set LF=^


::Above 2 blank lines are critical - do not remove

::Define CR variable containing a carriage return (0x0D)
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"


You can't use the PROMPT or FORFILES to get a CR or LF assigned to a variable.

Re: I there a way for save the result of Prompt $_

Posted: 29 Mar 2013 22:02
by carlsomo
I used this in a script to write 'set LF=^' to a line in a file without a trailing space:

Code: Select all

for /f "tokens=2* delims=÷" %%a in ('"for %%b in (1) do rem ÷set LF=^÷"') do echo(%%a>>"%temp%\file.ext


I'm not sure if that is what you are looking for but it worked for my purposes.

Carl

Re: I there a way for save the result of Prompt $_

Posted: 31 Mar 2013 10:20
by Aacini
carlos wrote:Hello. I want the way of save the output of command PROMPT $_ this is a new line character.


Hi Carlos, two points here:

- The PROMPT $_ does NOT produce a LF character, but a CR+LF pair.
- There is NO WAY to get a LF character with a FOR command. You may test this via a JScript or VBS file that output a LF char (with or without other delimiters) and trying to get it wih a FOR: don't works! :cry:

Antonio

Re: I there a way for save the result of Prompt $_

Posted: 31 Mar 2013 16:48
by Sponge Belly
Hi Carlos!

Borrowing from Walid2mi’s technique for splitting a CRLF pair (see seventh answer), and Jeb’s explanation of how newline variable hack works, I came up with this…

Code: Select all

@echo off & setlocal enableextensions

set lftmp=%tmp%\lf.tmp
echo(| (pause >nul & findstr "^" >%lftmp%)
<nul set /p ".=#" >>%lftmp%
set /p line= <%lftmp%
del /q %lftmp%
set ^"lf=^%line:~0,1%%line:~0,1%^"
set ^"nl=^^^%lf%%lf%^%lf%%lf%^"
echo(hello%nl%world

endlocal & exit /b 0


This should work, even if extra whitespace is accidentally added to the ends of lines by badly-written forum software or sloppy copying and pasting.

Happy Easter! :-)

- SB

Re: I there a way for save the result of Prompt $_

Posted: 01 Apr 2013 19:58
by carlos
Thanks for the replies.

This is my optimized code for the code of Sponge Belly:

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion

Set "LFFILE=LF.Tmp"
Echo(|(Pause >Nul &Findstr "^" >%LFFILE%)
Set /P "=#" >>%LFFILE% <Nul
Set /P LF=<%LFFILE%
(Set LF=^%LF:~0,1%%LF:~0,1%)

Echo(This is a!LF!new line


Re: I there a way for save the result of Prompt $_

Posted: 01 Apr 2013 20:59
by foxidrive
LF can be set using delayed expansion in a more straight forward way.

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion

Set "LFFILE=LF.Tmp"
Echo.|(Pause>Nul & Findstr "^" >%LFFILE%)
Set /P "=#" >>%LFFILE% <Nul
Set /P "LF=" <%LFFILE%
Set "LF=!LF:~0,1!"

Echo This is a!LF!new line
del %LFFILE%
pause


EDIT: added carlos' improvements in the post below

Re: Another way to create a line feed variable

Posted: 01 Apr 2013 21:26
by carlos
thanks foxidrive.
I will only sorround in quotes these lines:

Code: Select all

Set /P "LF=" <%LFFILE%
Set "LF=!LF:~0,1!"

Re: Another way to create a line feed variable

Posted: 19 Oct 2014 10:18
by Sponge Belly
Hello All! :)

In a post on another topic, Dave Benham observed:

SET /P will terminate the input upon reading a NULL byte. What really blows me away is that SET /P can read both newline (0x0A) and carriage return (0x0D) into a variable if they are followed by 0x00.


With the caveat that there has to be a non-control character (not necessarily immediately) following the null character.

Anyways, my point is, Dave’s discovery makes it even easier to create a forum-friendly LF variable:

Code: Select all

$ rem forum-friendly LF variable
$ cmd /d /u /c echo(^&echo(#|>nul (pause & pause & findstr "^" >lf.tmp)
$ set /p lf= <lf.tmp
$ (set nl=^^^%lf%%lf%^%lf%%lf%)
$ echo(words%nl%separated%nl%by%nl%newlines
words
separated
by
newlines

$ rem create file containing single null character
$ >lf.tmp cmd /d /u /c echo(
$ findstr /lv %nl% lf.tmp >ascii0.tmp
$ del lf.tmp


And once %nl% is defined, creating a file containing a single null character is easy.

HTH! :)

- SB