ForEntireLine

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

ForEntireLine

#1 Post by Ed Dyreen » 06 Jun 2011 05:56

Can someone link me to where I can study this technique ?
I tried not to post a question about it. And find it myself. I failed.
I know the LineFeed. I'm just not that bright :cry:

set forEmacro=^^^^^^^"eol^^^^=^^^^^^^%LF%%LF%^%LF%%LF%^^^%LF%%LF%^%LF%%LF%^^^^ delims^^^^=^^^^^^^"

What's the purpose of this ?

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: ForEntireLine

#2 Post by jeb » 06 Jun 2011 06:53

It's simply the way to set the EOL-character to <linefeed> so it is effectivly disabled.

It looks a bit complicated, as Dave decide to write it without helper variables.

In the end it is expanded to

Code: Select all

FOR /F "eol=<LF> delimns=" ...

Read more about this, at Re: Sorting tokens within a string

jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: ForEntireLine

#3 Post by dbenham » 06 Jun 2011 09:08

EdDyreen wrote:What's the purpose of this ?

It is useful for building macros. It makes it convenient to build a macro with a FOR /F that is guaranteed to read the entire line (no eol, no delims).

Here is a little demo

Code: Select all

@echo off
setlocal
set lf=^


:: Here is a macro with a FOR /F that is guaranteed to preserve the entire line
:: because both eol and delims are effectively disabled. It works, but it is
:: ugly, hard to code and hard to read
::
set simpleMacro1=for /f ^^^"eol^^=^^^%lf%%lf%^%lf%%lf%^^ delims^^=^^^" %%a in (";Please preserve this line") do echo %%a
echo Results of simpleMacro1:
%simpleMacro1%


:: I want to build a simple macro that makes the coding easier
set macro_ForEntireLine=^^^^^^^"eol^^^^=^^^^^^^%lf%%lf%^%lf%%lf%^^^%lf%%lf%^%lf%%lf%^^^^ delims^^^^=^^^^^^^"

:: Now I can build my macro much easier. It reads well and gives the same results
set simpleMacro2=for /f %macro_ForEntireLine% %%a in (";Please preserve this line") do echo %%a
echo:
echo Results of simpleMacro2:
%simpleMacro2%

echo:
echo As was the goal, both macros end up with identical definitions:
set simpleMacro

demo results:

Code: Select all

Results of simpleMacro1:
;Please preserve this line

Results of simpleMacro2:
;Please preserve this line

As was the goal, both macros end up with identical definitions:
simpleMacro1=for /f ^"eol^=^

^ delims^=^" %a in (";Please preserve this line") do echo %a
simpleMacro2=for /f ^"eol^=^

^ delims^=^" %a in (";Please preserve this line") do echo %a


Dave
Last edited by dbenham on 29 Jul 2011 12:49, edited 1 time in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: ForEntireLine

#4 Post by Ed Dyreen » 06 Jun 2011 09:13

Wow jeb, I'm beginning to understand it :D

Thanks to dbenham,

I didn't know that you can disable the eol-character, if it is one of the delims-characters.

But I suppose I found a way to disable the eol even if the delims is empty.

Code:
setLocal EnableDelayedExpansion
set lf=^


for /F ^"eol^=^

delims^="" %%a in ("^^caret!lf!;semicolon!lf! space!lf!""quote") do echo '%%a'

It is obvious that the eol should be a <LF>, but as the FOR /F splits at each <LF>, it is the same as eol would be empty.

jeb

But you couldn't have found this by simply guessing, this requires insight !!!

I've been sitting in front of DOS for over 15years & I never figured it out !

Still there's one thing I don't understand, why doesn't this simple test splits to a & b ?

Code: Select all

@echo off &setLocal EnableDelayedExpansion

for /F "delims=""" %%a in ("caret " " quote") do echo 'a=%%a' 'b=%%b'
for /F "delims="^" %%a in ("caret " " quote") do echo 'a=%%a' 'b=%%b'

pause
exit /b

It seems delims refuses to accept " as a valid delimiter ?!

Wow dBenham, what a clever variation :shock:

Code: Select all

@echo off
setlocal disableDelayedExpansion
set "str=skip 1st line,^caret,;semicolon, space,"quote,!exclamation"
setLocal EnableDelayedExpansion
set ^"str=!str:,=^

!"
echo str=!str!
echo:----------------
for /F ^"usebackq^ skip^=1^ eol^=^

delims^=^" %%a in ('!str!') do (
  setlocal disableDelayedExpansion
  echo '%%a'
  endlocal
)
echo:---------------
setlocal disableDelayedExpansion
for /f ^"eol^=^

delims^=^" %%a in (";eol disabled so line preserved") do echo %%a


The fact that it's even possible :P

Code: Select all

set ^"str=!str:,=^

!"


Ok dBenham now let's study your above post..... hmmm ... hmm ...
It is useful for building macros. It makes it convenient to build a macro with a FOR /F that is guaranteed to read the entire line (no eol, no delims).


dBenham, you know how long I've been looking for someting like this, I bet you were too :P
I just need to know one other thing, is it really bulletproof, have you tested this thoroughly with strings like:
" hjfjkfdfhj333""'" " " | | && || >> << ^^" " " " "8765 àç!è( "
I bet the quotes have to be even or it will still fail :o
So we still have to pre-prepare the input.

I am going to experiment with this... 8)

" hjfjkfdfhj333""'" " " | | && || >> << ^^" " " " "8765 àç!è( "
Whaa that didn't work, I'm always way to optemistic. Only a fool like me would try this. :mrgreen:

@dBenham Euh, I guess you are already trained at this, do you know of a variation for EnabledDelayedExpansion :?:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: ForEntireLine

#5 Post by dbenham » 06 Jun 2011 20:49

EdDyreen wrote:I just need to know one other thing, is it really bulletproof, have you tested this thoroughly with strings like:
" hjfjkfdfhj333""'" " " | | && || >> << ^^" " " " "8765 àç!è( "
I bet the quotes have to be even or it will still fail
So we still have to pre-prepare the input.
It works just fine, no prep required

test.txt

Code: Select all

;Please don't ignore me!
   This is all good!
Follow the !path! of enlightenment.
" hjfjkfdfhj333""'" " " | | && || >> << ^^" " " " "8765 àç!è( "
"&"&"^"^">">"<"<"|"|"("(")")"

test.bat (macro_RtnLib.bat is in my return ANY string safely and easily post)

Code: Select all

@echo off
setlocal
if not defined macro\load.macro_RtnLib call macro_RtnLib
set n=0
set macro1=for /f %macro_ForEntireLine% %%s in (test.txt) do (%\n%
  set /a "n+=1"%\n%
  set "ln=%%s"%\n%
  setlocal EnableDelayedExpansion%\n%
  echo line!n!=!ln!%\n%
  endlocal%\n%
)
%macro1%

results:

Code: Select all

line1=;Please don't ignore me!
line2=   This is all good!
line3=Follow the !path! of enlightenment.
line4=" hjfjkfdfhj333""'" " " | | && || >> << ^^" " " " "8765 àç!è( "
line5="&"&"^"^">">"<"<"|"|"("(")")"


Dave

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: ForEntireLine

#6 Post by Cleptography » 06 Jun 2011 22:17

Ok, with all due respect and as polite as I can be.
My dos is not nearly this advanced nor do I know if I would ever want it to be.
I understand what is happening here, but what is the purpose?
I get one thing that the macros help to speed things up, but is that it?
What are these good for?
Thank You.
:oops:
My other question why can't we just use doskey for this...
DOSKEY supports approximately 61900 bytes. Due to an error in the DOSKEY program that causes DOSKEY to mark memory as unused when it terminates and stays resident, specifying a buffer size larger than this amount causes the computer to hang.

Or why don't you just write something that fixes this.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: ForEntireLine

#7 Post by Ed Dyreen » 07 Jun 2011 04:21

We are simply trying to evaluate a line of text without for corrupting our line :!:
This has nothing to do with Doskey.

macros help to speed things up thats all they are good for yes but still:
$MacroSpeed = $FunctionSpeed \ $Divider
$Divider get's larger if filesize get's larger !
[SOLVED] SET /a -- Random Number? :arrow:
http://www.dostips.com/forum/viewtopic.php?f=3&t=1817

Post Reply