Another way to create a line feed variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Another way to create a line feed variable

#1 Post by carlos » 29 Mar 2013 16:02

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 29 Mar 2013 19:20

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.

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

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

#3 Post by carlsomo » 29 Mar 2013 22:02

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#4 Post by Aacini » 31 Mar 2013 10:20

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

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

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

#5 Post by Sponge Belly » 31 Mar 2013 16:48

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

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

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

#6 Post by carlos » 01 Apr 2013 19:58

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


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#7 Post by foxidrive » 01 Apr 2013 20:59

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

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Another way to create a line feed variable

#8 Post by carlos » 01 Apr 2013 21:26

thanks foxidrive.
I will only sorround in quotes these lines:

Code: Select all

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

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Another way to create a line feed variable

#9 Post by Sponge Belly » 19 Oct 2014 10:18

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

Post Reply