Convert CRLF to LF

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Convert CRLF to LF

#1 Post by RMSoares » 10 Aug 2012 05:23

I'm sending a file generated in Windows to a Linux machine, I need to replace the line terminator 'CRLF' the terminator 'LF'.

How can I do this?

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

Re: Convert CRLF to LF

#2 Post by Squashman » 10 Aug 2012 05:41

I usually use a DOS2UNIX utility. Plenty of them out there.
http://dos2unix.sourceforge.net/

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

Re: Convert CRLF to LF

#3 Post by Ed Dyreen » 10 Aug 2012 07:05

[edit]
This CRLFtoLF conversion fails if the source contains single linefeeds, they are both converted and appended at the end :(

Code: Select all

@echo off &setlocal disableDelayedExpansion

set "$iFile=CRLFtoLF.TXT"
set "$oFile=ou.TXT"

set $lf=^


::
for /f %%? in (

   '2^>nul ^( ^< "%$iFile%" find.EXE /c /v "" ^)'

) do    set "$end=%%?"

echo.linefeeds[%$end%]

< "%$iFile%" (

   for /l %%! in (

      1, 1, %$end%

   ) do    set "$=" &set /p "$=" &(

      setlocal enabledelayedexpansion
      rem (
         (<nul set /p ="!$!!$lf!")
      rem )
      endlocal
   )

) > "%$oFile%"

echo.
echo.'%$oFile%'
type "%$oFile%"

pause
exit
LFtoCRLF is easy

Code: Select all

@echo off &setlocal disableDelayedExpansion

set "$iFile=LFtoCRLF.TXT"
set "$oFile=ou.TXT"

(for /f ^"usebackqeol^=^

delims^=^" %%? in (

       "%$iFile%"

) do   (echo(%%?)

)> "%$oFile%"

echo.
echo.'%$oFile%'
type "%$oFile%"

pause
exit
Yet this post shows how to read in hex and this post shows you can convert :hex2str But I never needed it so I stop here :)

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

Re: Convert CRLF to LF

#4 Post by foxidrive » 10 Aug 2012 08:21

Ed Dyreen wrote:LFtoCRLF is easy


For less than 65k lines, it is indeed.

Code: Select all

more a.htm >b.htm

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: Convert CRLF to LF

#5 Post by RMSoares » 10 Aug 2012 10:54

Hi,
i'm running the fowlling script, but it return an empty file.

Any Help ?

Code: Select all

@echo off &setlocal disableDelayedExpansion

set "$iFile=c:\TEMP\A.txt"
set "$oFile=c:\TEMP\B.txt"

set $lf=^


::
for /f %%? in (
   '2^>nul ^( ^< "%$iFile%" find.EXE /c /v "" ^)'
) do    set "$end=%%?"
echo.linefeeds[%$end%]
< "%$iFile%" (
   for /l %%! in (
      1, 1, %$end%
   ) do    set "$=" &set /p "$=" &(
      setlocal enabledelayedexpansion
     rem (
         (<nul set /p ="!$!!$lf!")
      rem )
      endlocal
   )
) > "%$oFile%"

echo.
echo.'%$oFile%'
type "%$oFile%"

pause
exit

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Convert CRLF to LF

#6 Post by Liviu » 10 Aug 2012 13:35

RMSoares wrote:i'm running the fowlling script, but it return an empty file.

Ed's code works nicely here. What is the contents of your c:\TEMP\A.txt file, and what is displayed in the console after you run the given code?

Liviu

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

Re: Convert CRLF to LF

#7 Post by foxidrive » 10 Aug 2012 13:48

Ed's code has the usual problem of removing blank lines.

If exact duplication of the file is required then this is best left to a dedicated utility, there are many to be found in google.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Convert CRLF to LF

#8 Post by Liviu » 10 Aug 2012 14:22

foxidrive wrote:Ed's code has the usual problem of removing blank lines.

That one is fixable, just replace Ed's line with...

Code: Select all

      rem (
         if defined $ (<nul set /p ="!$!!$lf!") else (<nul set /p "=!$lf!")
      rem )

foxidrive wrote:If exact duplication of the file is required then this is best left to a dedicated utility, there are many to be found in google.

Agreed. Batch solutions also have size limitation on the line length, drop control characters at the end of lines, and 'find /c' miscounts one less empty line at the end of the file.

Liviu

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

Re: Convert CRLF to LF

#9 Post by foxidrive » 10 Aug 2012 14:43

Liviu wrote:
foxidrive wrote:Ed's code has the usual problem of removing blank lines.

That one is fixable, just replace Ed's line with...

Code: Select all

      rem (
         if defined $ (<nul set /p ="!$!!$lf!") else (<nul set /p "=!$lf!")
      rem )



It still drops blank lines, Liviu

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Convert CRLF to LF

#10 Post by Liviu » 10 Aug 2012 15:02

foxidrive wrote:It still drops blank lines

That's odd. It certainly doesn't drop them here. With the one line change I posted, my test case gives...

Code: Select all

C:\tmp>type crlftolf.txt

line 2


line 5

"! % < " ( " > " ) ^ "

line 9

C:\tmp>crlftolf.cmd
linefeeds[9]

'ou.TXT'

line 2


line 5

"! % < " ( " > " ) ^ "

line 9

C:\tmp>
...and the ou.txt file has all the right LFs upon hex inspection.

Just to be sure, did you paste the line literally? The "else" part has the opening quote placed before the "=" sign, and that's deliberate. Or maybe it's just an isolated quirk of my XP sp3.

Either way, the issue itself of dropping blank lines is on the output side. On the input side, blank lines are read in correctly. Only remaining problem is how to send a single LF character to the output stream. If (<nul set /p "=!$lf!") doesn't work reliably under all windows, then perhaps someone can come up with a better/universal alternative.

Liviu

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

Re: Convert CRLF to LF

#11 Post by foxidrive » 10 Aug 2012 15:11

Yes, (<nul set /p "=!$lf!") doesn't work in Windows 7.

I used this and it gave me the extra lines, but with the 'a'

(<nul set /p "=a!$lf!")

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Convert CRLF to LF

#12 Post by Liviu » 10 Aug 2012 16:46

foxidrive wrote:Yes, (<nul set /p "=!$lf!") doesn't work in Windows 7.

Indeed. Thanks for (re)pointing. Apparently, the behavior changed since Vista http://www.dostips.com/forum/viewtopic.php?p=16857#p16857.

For the case here it would be relatively straightforward to count/accumulate the empty lines and prepend the outstanding LFs to the next non-empty line. This would still miss any trailing empty lines at the end of the file, but that's unavoidable as long as no way is found to reliably emit single LFs.

Liviu

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

Re: Convert CRLF to LF

#13 Post by Ed Dyreen » 10 Aug 2012 23:10

foxidrive wrote:Ed's code has the usual problem of removing blank lines.
in.TXT

Code: Select all

this[0A]^wo rks!



";Please <preserve=^"this line"!>"[0A][0A][0A][0A]This
^wo rks!
CRLFtoLF.CMD

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0

set "$iFile=in.TXT"
set "$oFile=ou.TXT"

set $lf=^


::
for /f "delims=#" %%? in ( '"prompt #$H# &echo on &for %%b in (1) do rem"' ) do set "$BS=%%?"


echo.¯ '%$iFile%'
type "%$iFile%" &echo.
echo.® '%$iFile%'

for /f %%? in (

       '2^>nul ^( ^< "%$iFile%" find.EXE /c /v "" ^)'

) do   set "$end=%%?"

< "%$iFile%" (

       for /l %%! in (

              1, 1, %$end%

       ) do   set "$=" &set /p "$=" &(

              setlocal enabledelayedexpansion
              rem (  <nul set /p ="_!$BS!!$!!$lf!" >&2
                     <nul set /p ="_!$BS!!$!!$lf!"
              rem )
              endlocal
       )

) > "%$oFile%"
echo.

echo.¯ '%$oFile%'
type "%$oFile%" &echo.
echo.® '%$oFile%'

echo.linefeeds[%$end%]
pause
exit

Code: Select all

» 'in.TXT'
this
^wo rks!



";Please <preserve=^"this line"!>"



This
^wo rks!
« 'in.TXT'

» 'ou.TXT'
this
^wo rks!



";Please <preserve=^"this line"!>"



This
^wo rks!






« 'ou.TXT'
linefeeds[11]
Another problem is that find.EXE counts LFs AND CRLF yet the set /p doesn't stops reading when it encounters an LF. But that can be solved by counting the numbers of LFs read by the set /p and decrementing the loop on the fly. 8)

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Convert CRLF to LF

#14 Post by Liviu » 10 Aug 2012 23:31

Ed Dyreen wrote:
foxidrive wrote:Ed's code has the usual problem of removing blank lines.
in.TXT

Sorry, but I don't follow. Running your example, the screen looks fine, but the saved ou.txt file has the literal BACKSPACE 0x08 embedded, which defeats the purpose here. As I see it, jeb's comment "this only works with display output, but not with file output" (http://www.dostips.com/forum/viewtopic.php?p=16857#p16857) still stands unanswered.

Liviu

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

Re: Convert CRLF to LF

#15 Post by Ed Dyreen » 11 Aug 2012 10:22

[edit Aug 2012 13:34]
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sorry, but I don't follow. Running your example, the screen looks fine, but the saved ou.txt file has the literal BACKSPACE 0x08 embedded, which defeats the purpose here. As I see it, jeb's comment "this only works with display output, but not with file output" (viewtopic.php?p=16857#p16857) still stands unanswered.

Liviu
I didn't test it properly :oops: but it seem to work now :mrgreen:

in.TXT

Code: Select all

this[0A]^wo rks!



";Please <preserve=^"this line"!>"[0A][0A][0A][0A]This
^wo rks!
ou.TXT

Code: Select all

this[0A]^wo rks![0A][0A][0A][0A]";Please <preserve=^"this line"!>"[0A][0A][0A][0A]This[0A]^wo rks!
console

Code: Select all

» 'in.TXT'
this
^wo rks!



";Please <preserve=^"this line"!>"



This
^wo rks!
« 'in.TXT'

» 'ou.TXT'
this
^wo rks!



";Please <preserve=^"this line"!>"



This
^wo rks!
« 'ou.TXT'
linefeeds[10]
Druk op een toets om door te gaan. . .
I've been fighting with the '(<nul set /p =!$!)' but it kept getting confused on the escape character inside '";Please <preserve=^"this line"!>"' even with the delayed expansion :shock: I just couldn't get it right on XP so, I ended up using '(echo(!$!)' but this required me to push over endlocal.
But then suddenly the '(<nul set /p =!$!)' works again :?:

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0

set "$iFile=in.TXT"
set "$oFile=ou.TXT"





prompt $G &<nul set /p "= " &chcp 850 &color 0B %=      prompt minimalistic, codepage 850, colorConsole      =%
:: --------------------------------------------------------------------------------------------------------------------------
:: - %* callback script validation                                                                                          -
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 2012^08^08
:: (
   set "$notDelayedFlag=!"          %=    persist over endlocal               =%
:: )
:: --------------------------------------------------------------------------------------------------------------------------

:: --------------------------------------------------------------------------------------------------------------------------
echo. &echo. ¯ offDelayed definitions
:: --------------------------------------------------------------------------------------------------------------------------
setlocal disableDelayedExpansion
:: (

:: --------------------------------------------------------------------------------------------------------------------------
echo. &echo. ¯ echon_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
::
:: on channel 1: echo with LineFeed
:: (
%=   =%set "echon_=echo("
:: )
echo. ® echon_ [ok:loaded]
::
( >con echo. &%echon_% This &%echon_%Works ) 2>nul
:: --------------------------------------------------------------------------------------------------------------------------
:: --------------------------------------------------------------------------------------------------------------------------
%echon_% &%echon_% ¯ nechon_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
:: support            : naDelayed
::
:: on channel 1: LineFeed, echo with LineFeed
:: (
%=   =%set "nechon_=%echon_%&%echon_%"
:: )
%echon_% ® nechon_ [ok:loaded]
::
( %nechon_% This &%nechon_%Works ) 2>nul
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
echo.endoftest
pause
exit
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%nechon_% ¯ echo_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
::
:: on channel 1: echo without LineFeed
:: (
%=   =%set "echo_=<nul set/p="
:: )
%echon_% ® echo_ [ok:loaded]
::
( %echo_% This &%echo_% Works &echo. ) 2>nul
:: --------------------------------------------------------------------------------------------------------------------------
:: --------------------------------------------------------------------------------------------------------------------------
%nechon_% ¯ necho_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
:: support            : naDelayed
::
:: on channel 1: LineFeed then echo without LineFeed
:: (
%=   =%set "necho_=%echon_%&%echo_%"
:: )
%necho_% ® necho_ [ok:loaded]
::
( %necho_% This &%necho_%Works &echo. ) 2>nul
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
echo.endoftest
pause
exit
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%echon_% &%necho_% ¯ n2echo_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
:: support            : naDelayed
::
:: on channel 1: 2 LineFeeds then echo without LineFeed
:: (
%=   =%set "n2echo_=%echon_%&%necho_%"
:: )
%necho_% ® n2echo_ [ok:loaded]
::
( %n2echo_% This &%n2echo_% Works &echo. ) 2>nul
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
echo.endoftest
pause
exit
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%n2echo_% ¯ n2echon_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
:: support            : naDelayed
::
:: on channel 1: 2 LineFeeds, echo with LineFeed
:: (
%=   =%set "n2echon_=%nechon_%&%echon_%"
:: )
%necho_% ® n2echon_ [ok:loaded]
::
( %n2echon_% This &%n2echon_%Works ) 2>nul
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
echo.endoftest
pause
exit
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%necho_% ¯ pre_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
::
:: (
%=   =%set "pre_=%n2echo_% ¯"
:: )
%necho_% ® pre_ [ok:loaded]
:: --------------------------------------------------------------------------------------------------------------------------
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% post_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
::
:: (
%=   =%set "post_=%necho_% ®"
:: )
%post_% post_ [ok:loaded]
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
echo.endoftest
pause
exit
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% $lf Asci-0x0A, LineFeed naDelayed
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 11/04/2012
::
:: The first set/p reads the first line, in this case the "@echo off".
:: The pause reads the next character, in this case the next line is empty, so it reads a <CR>.
:: The next set/p reads now the <LF> and the next line "<"%~f0" >nul (set/pLF=&pause&set/pLF=)"
:: (
   setlocal enableDelayedExpansion
   :: (
      <"%~f0" >nul (set/p$lf=&pause&set/p$lf=)
   :: )
   for %%r in ( "!$lf:~0,1!" ) do endlocal &set "$lf=%%~r" &echo. &echo.This%%~rworks
:: )
%post_% $lf Asci-0x0A [ok:loaded]
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
echo.endoftest
pause
exit
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% endoftest
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 20/05/2012
:: support            : naDelayed
::
:: on console; notify the end of a test, terminates script !
:: (
%=   =%set "endoftest=(%necho_% endoftest &pause&exit 0)>con"
:: )
%post_% endoftest [ok:loaded]
::
goto :skip "()"
:: (
   %endoftest%
:: )
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% $cr Ascii-0x0D, only for DelayedExpansion
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 13/07/2012
:: (
   for /f %%? in ( 'copy /Z "%~f0" nul' ) do set "$cr=%%?"
:: )
%post_% $cr Ascii-0x0D, only for DelayedExpansion [ok:loaded]
::
setlocal enableDelayedExpansion &%nechon_% This!$cr! Works &endlocal
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% $n1c, newLine+continuation, 1 expansions, 0sets, 1echo
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
:: (
%=   =%set ^"$n1c=^^^%$lf%%$lf%^%$lf%%$lf%^^"
:: )
%post_% $n1c, newLine+continuation, 1 expansions, 0sets, 1echo [ok:loaded]
::
%necho_%--this%$n1c%
works--
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% $n2c newLine+continuation, 2 expansions, 1sets, 1echo
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 08/03/2012
:: (
%=   =%set ^"$n2c=^^^^^^^%$lf%%$lf%^%$lf%%$lf%^^^%$lf%%$lf%^%$lf%%$lf%^^"
:: )
%post_% $n2c newLine+continuation, 2 expansions, 1sets, 1echo [ok:loaded]
::
setlocal
:: (
   set var=this%$n2c%
works
   %nechon_% ---- &call %echo_% %%var%%
:: )
endlocal
:: --------------------------------------------------------------------------------------------------------------------------
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% forC_, counter
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
::
:: loop token, counting
:: (
%=   =%set "forC_=for /l %%! in"
:: )
%post_% forC_ [ok:loaded]
::
%n2echon_% %forC_%
:: --------------------------------------------------------------------------------------------------------------------------
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% forQ_, looper
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 31/01/2012
::
:: loop token, simple form
:: (
%=   =%set "forQ_=for %%? in"
:: )
%post_% forQ_ [ok:loaded]
::
%n2echon_% %forQ_%
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% ForEntireLine
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 22/02/2012
:: support            : naDelayed
:: (
%=   =%set  ^"forLineR_=for /f ^^^"eol^^=^^^%$lf%%$lf%^%$lf%%$lf%delims^^=^^^" %%r in"
%=   =%set ^"mForLineR_=for /f ^^^^^^^"eol^^^^=^^^^^^^%$lf%%$lf%^%$lf%%$lf%^^^%$lf%%$lf%^%$lf%%$lf%delims^^^^=^^^^^^^" %%r in"
:: )
%post_% ForEntireLine
::
setlocal
:: (
   %n2echo_% a for that always returns the entire line, 1 expansions, 0sets, 1echo
   ::
        %necho_% forLineR_: &%forLineR_% ( ";Please <preserve=^"this line"!>" ) do %echo_% %%r

   %n2echo_% a for that always returns the entire line, 2 expansions, 1sets, 1echo
   ::
   set @MforLineR=%mForLineR_% ( ";Please <preserve=^"this line"!>" ) do %echon_% %%r
        %necho_% @MforLineR: &%@MforLineR%
:: )
endlocal
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% delim_, alphanumeric [a-z], delims [default delimiters]
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 2012^08^09
:: support            : problematic chars: [!,",%,&,(,),<,=,>,?,^,|]
::                      encoded chars    : [*]
:: warn               : variable's contents are used in the splitting process.
::                      do not pass variables with malformatted content !
:: (

:: support            : onDelayed, byVal¦eVal, delayedChars, paramless input
:: (
%=   =%set ^"oMDelim_=%forQ_% (1,2) do if %%?==2 (%$n2c%
%=      =%set $=!$:42#=4\2#!^^^&set $=!$:^^^"=""!%=                pre-delay     =%%$n2c%
%=      =%set $=!$:\*=42#!^^^&set "$=!$:^=^^^^!"^^^&call set "$=%%^$:^!=#"#"^!%%"!%=    delay 2-times    =%%$n2c%
%=      =%set "$=!$:#"#"=^^^!"^^^&set $=!$:""=^^^"!%=                post-delay    =%%$n2c%
%=      =%set ?=^^^&%forQ_% (!$!) do set ?=!?!¦%%?%$n2c%
%=      =%if +!?!==+ set ?=""%$n2c%
%=      =%set ?=!?:42#=\*!^^^&set ?=!?:4\2#=42#!%$n2c%
%=   =%)^^^&for /f "tokens=1-26delims=¦" %%a in ("!?!") do "
:: )

:: )
%post_% Delim_, alphanumeric [a-z], delims [default delimiters]
::
setlocal disableDelayedExpansion
:: (
   set "$var=This\*^Works !"

   setlocal disableDelayedExpansion
   :: (
%=      =% set ^"@macro_=%oMDelim_%(%$n1c%

%=         =%echo.%$n1c%
%=         =%echo. oMDelim_:%$n1c%
%=         =%echo. a=%%~a=^^^!%%~a^^^!_%$n1c%
%=         =%echo. b=%%~b=^^^!%%~b^^^!_%$n1c%
%=         =%echo. c=%%~c=^^^!%%~c^^^!_%$n1c%

%=      =%endlocal)else setlocal enableDelayedExpansion^&set $="
      ::
      ( %@macro_% $var, "This ^^Works ^!", "!$var!" )
   :: )
   endlocal
:: )
endlocal
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% macroStart_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 2012^08^10
:: support            : naDelayed
::
:: Retrieves current DelayedFlag needed by macroEnd and sets localscope.
:: (
%=   =%set ^"macroStart_=%oMDelim_%(%$n1c%

%=      =%(echo.^&if defined $notDelayedFlag (set/p=NotDelayed^<nul)else set/p=Delayed^<nul)^>^&2%$n1c%
%=      =%if defined $details (set $fetch=, !$details!)else set $fetch=%$n1c%
%=      =%echo.^&echo.^&set/p= ¯ !$defines!!$fetch!^<nul%$n1c%
%=      =%endlocal^&set $details=^&setlocal %%~a%$n1c%

%=   =%)else set $notDelayedFlag=!^&setlocal enableDelayedExpansion^&set $="
:: )
%post_% macroStart_
::
setlocal
:: (
   set "$defines=@This_Works" &set "$details=Some detail"
   ( %macroStart_% enableDelayedExpansion  ) &endlocal
   ( %macroStart_% "disableDelayedExpansion" ) &endlocal
:: )
endlocal
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% endlocalR_
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 30/01/2012
:: support            : onDelayed
:: style              : dBenham's replace
::
:: push over endlocal using replace technique
:: (
%=   =%set ^"endlocalR_=(set ^"$replace=%% ^" !$cr!!$cr!^"%$n1c%
%=   =%)^&for /f "tokens=1-3" %%1 in ("!$replace!") do for %%4 in ("!$lf!") do endlocal^&endlocal^&"
:: )
%post_% endlocalR_
::
setlocal enabledelayedexpansion
:: (
   setlocal &setlocal &%endlocalR_% set "$cr=%%3"   !
   ::
   %necho_% $cr failed !$cr! $cr succes

   setlocal &setlocal
   :: (
      echo.
   :: )
   %endlocalR_% ( %=                      skip a line to max return size to 8k   =%
      ::
      set "$var=one=%%1_%%~4 two=%%2_"   !
   )
   ::
   %necho_% !$var!
:: )
endlocal
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% macroEnd
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 03/02/2012
:: support            : naDelayed, $cr, $lf, theoretical max 8192, real max 8192-10, replacer practical max 8192-+-1965
:: style              : dBenham's replace
::
:: Prepares $defined to be pushed over endlocal using immediate expansion + replace technique,
:: regardless the outer delayed setting
:: (
%=   =%set ^"macroEnd=(setlocal enableDelayedExpansion%$n1c%

%=      =%(echo.^&if defined $notDelayedFlag (set/p= NotDelayed^<nul)else set/p= Delayed^<nul)^>^&2%$n1c%
%=      =%%forQ_% ("!$defines!") do set $=^&set $=!%%~?!^&^&(%$n1c%
%=         =%(echo.^&set/p= ^<nul^&set %%~?)^>^&2%$n1c%
%=         =%set $1=!$:~0,4096!^&set $2=!$:~4096!^&%forQ_% (1,2) do if defined $%%~? (%$n1c%
%=            =%set $=!$%%~?!^&set $=!$:%%=%%1!^&set ^"$=!$:^"=%%2!^"%=   Replace by injection=%%$n1c%
%=            =%for %%r in ("!$cr!") do set $=!$:%%~r=%%3!%$n1c%
%=            =%for %%r in ("!$lf!") do set $=!$:%%~r=%%~4!%$n1c%
%=            =%if not defined $notDelayedFlag call set "$=%%^$:^!=""^!%%"!^&set "$=!$:^=^^!"^&set "$=!$:""=^!"%$n1c%
%=            =%set $%%~?=!$!%$n1c%
%=         =%)%$n1c%
%=      =%)^|^|call :§endSimple "macroEnd, not defined: '!$defines!'"%$n1c%
%=      =%set $=^&set $=set "!$defines!=!$1!!$2!"^!^|^|call :§endSimple "macroEnd, variable overflow: '!$defines!'"%$n1c%
%=      =%(echo.^&set/p= !$!^<nul)^>^&2%$n1c%
%=      =%echo.^&set/p"= ® !$defines! [ok:loaded]"^<nul%$n1c%

%=   =%)"
:: )
%post_% macroEnd
:: (
   setlocal enableDelayedExpansion &set "$defines=$8kVar"
   :: (
      %n2echo_% testing replacer practical max 8192-+-1965

      2>nul ( %macroStart_% enableDelayedExpansion )
      :: (
         set "$8kVar=_##^^#"q"ex^!#%%1#_" %=    16 chars initially =%
         for /l %%! in ( 1, 1, 11 ) do set "$8kVar=!$8kVar!!$8kVar!"
         set "$8kVar=!$8kVar!!$8kVar:~0,-1965!"
      :: )
      %macroEnd%
      %endlocalR_% (
%=      =%%$%
      )

      %n2echo_% $8kVar=!$8kVar!_
   :: )
   endlocal

   setlocal enableDelayedExpansion &set "$defines=$var"
   :: (
      2>nul ( %macroStart_% enableDelayedExpansion )
      :: (
%=         =%set ^"$var=(%$n1c%
%=            =%echo. This "^^Works^!"%$n1c%
%=         =%)else setlocal enableDelayedExpansion ^&set $="
      :: )
      2>nul %macroEnd%
      %endlocalR_% (
%=      =%%$%
      )
      ::
      %n2echo_% $var=!$var!_
   :: )
   endlocal

   setlocal enableDelayedExpansion &set "$defines=$var"
   :: (
      2>nul ( %macroStart_% disableDelayedExpansion )
      :: (
%=         =%set "$var=echo. This "^^Works^!""
      :: )
      2>nul %macroEnd%
      %endlocalR_% (
%=      =%%$%
      )
      ::
      %n2echo_% $var=!$var!_
   :: )
   endlocal
:: )
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% advancedR
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 03/02/2012
:: support            : naDelayed, $cr, $lf
:: style              : dBenham's replace
::
:: Prepares multiple variables to be pushed over endlocal using immediate expansion, regardless the outer delayed setting
:: (
%=   =%set ^"advancedR=%forQ_% (1,2) do if %%?==2 (%$n1c%

%=      =%(echo.^&if defined $notDelayedFlag (set/p= NotDelayed^<nul)else set/p= Delayed^<nul)^>^&2%$n1c%
%=      =%%forQ_% (!$!) do set $=^&set $=!%%~?!^&^&(%$n1c%
%=         =%(echo.^&set/p= ^<nul^&set %%~?)^>^&2%$n1c%
%=         =%set $1=!$:~0,4096!^&set $2=!$:~4096!^&%forQ_% (1,2) do if defined $%%~? (%$n1c%
%=            =%set $=!$%%~?!^&set $=!$:%%=%%1!^&set ^"$=!$:^"=%%2!^"%=   Replace by injection =%%$n1c%
%=            =%for %%r in ("!$cr!") do set $=!$:%%~r=%%3!%$n1c%
%=            =%for %%r in ("!$lf!") do set $=!$:%%~r=%%~4!%$n1c%
%=            =%if not defined $notDelayedFlag call set "$=%%^$:^!=""^!%%"!^&set "$=!$:^=^^!"^&set "$=!$:""=^!"%$n1c%
%=            =%set $%%~?=!$!%$n1c%
%=         =%)%$n1c%
%=         =%(set/p= #%%~?=set "%%~?=!$1!!$2!"^!^<nul)^>^&2%$n1c%
%=         =%set #%%~?=^&set #%%~?=set "%%~?=!$1!!$2!"^!^|^|call :§endSimple "advancedR, variable overflow: '%%~?'"%$n1c%
%=      =%)^|^|call :§endSimple "%%~?: not defined."%$n1c%

%=   =%)else setlocal enableDelayedExpansion^&set $="
:: )
%post_% advancedR
:: (
   setlocal enableDelayedExpansion &set "$notDelayedFlag=!"
   :: (
      set "$var="
      setlocal enableDelayedExpansion
      :: (
         set "$var=This ^^works^!"
      :: )
      %advancedR% $var
      endlocal &endlocal &%#$var%

      echo. &<nul set/p"= " &set "$var"

      set "$var="
      setlocal disableDelayedExpansion
      :: (
         set "$var=This ^works!"
      :: )
      %advancedR% $var
      endlocal &endlocal &%#$var%

      echo. &<nul set/p"= " &set "$var"
   :: )
   endlocal

   setlocal disableDelayedExpansion &set "$notDelayedFlag=!"
   :: (
      set "$var="
      setlocal enableDelayedExpansion
      :: (
         set "$var=This ^^works^!"
      :: )
      %advancedR% $var
      endlocal &endlocal &%#$var%

      echo. &<nul set/p"= " &set "$var"

      set "$var="
      setlocal disableDelayedExpansion
      :: (
         set "$var=This ^works!"
      :: )
      %advancedR% $var
      endlocal &endlocal &%#$var%

      echo. &<nul set/p"= " &set "$var"
   :: )
   endlocal

   setlocal enableDelayedExpansion &set "$defines=$var"
   :: (
      set "$var="
      >nul 2>&1 ( %macroStart_% disableDelayedExpansion )
      :: (
%=         =%set "$var=echo. This "^^Works^!""
      :: )
      %advancedR% $var
      %endlocalR_% (
%=      =%%#$var%
      )
      ::
      %necho_% $var=!$var!_
   :: )
   endlocal
:: )
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
:: --------------------------------------------------------------------------------------------------------------------------
%pre_% Global return
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 30/01/2012
:: Warning            : Using global $notDelayedFlag, make sure it is correct !
:: (
   setlocal enableDelayedExpansion
   :: (
      %necho_% Globally called &if defined $notDelayedFlag (set/p=NotDelayed<nul)else set/p=Delayed<nul
      %necho_% Building Index...
%=      =%set $Index=^
%=      =%,$lf^
%=      =%,$cr^
%=      =%,$n1c^
%=      =%,$n2c^
%=      =%,echo_^
%=      =%,echon_^
%=      =%,nechon_^
%=      =%,necho_^
%=      =%,n2echo_^
%=      =%,pre_^
%=      =%,post_^
%=      =%,endoftest^
%=      =%,forC_^
%=      =%,forQ_^
%=      =%,forLineR_^
%=      =%,endlocalR_^
%=      =%,macroStart_^
%=      =%,macroEnd^
   :: )
   %necho_% Preparing involved variables for global return...
   ( %advancedR% !$Index! )
   %endlocalR_% ( endlocal %=                   skip a line to max return size to 8k    =%
      ::
%=      =%%#$lf%
%=      =%%#$cr%
%=      =%%#$n1c%
%=      =%%#$n2c%
%=      =%%#echo_%
%=      =%%#echon_%
%=      =%%#necho_%
%=      =%%#nechon_%
%=      =%%#n2echo_%
%=      =%%#pre_%
%=      =%%#post_%
%=      =%%#endoftest%
%=      =%%#forC_%
%=      =%%#forQ_%
%=      =%%#forLineR_%
%=      =%%#endlocalR_%
%=      =%%#macroStart_%
%=      =%%#macroEnd%
   )
:: )
%post_% Global return
:: (
   %n2echo_% Global $cr verification...

   setlocal enableDelayedExpansion &set "$defines=$var"
   :: (
      2>nul ( %macroStart_% enableDelayedExpansion )
      :: (
%=         =%set ^"$var=(%$n1c%
%=            =%!n2echo_! $cr failed ^^^!$cr^^^! $cr succes%$n1c%
%=         =%)"
      :: )
      %macroEnd%
      %endlocalR_% (
%=      =%%$%
      )
      ::
      %$var%
   :: )
   endlocal
:: )
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()

:: )
%post_% offDelayed definitions
:: --------------------------------------------------------------------------------------------------------------------------

:: --------------------------------------------------------------------------------------------------------------------------
set "$defines=endlocal_"
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 09/04/2012
:: support            : naDelayed, no ( $cr, $lf ), ( encoded or regular escaped * )
:: Warn               : for efficiency, the number of endlocals is controlled by global $eCount !
::
set ^"$usage.%$defines%=^
 use: ( %%endlocal_%% #eCount, #var, #etc.. )%$n1c%
 err: Unaffected, panic otherwise"
::
2>nul ( %macroStart_% enableDelayedExpansion )
:: (
%=   =%set ^"!$defines!=!forQ_! (1,2) do if %%?==2 (%$n1c%

%=      =%set/a$eCount+=1^&if ^^^!$eCount^^^! lss 2 set $eCount=2%$n1c%
%=      =%set $=^&!forQ_! (^^^!?^^^!) do (set ?=^^^!%%~?^^^!^&if defined ? set ?=^^^!?:34#=3\4#^^^!%$n1c%
%=         =%set $=^^^!$^^^!,34#%%~?=^^^!?^^^!34#%$n1c%
%=      =%)%$n1c%
%=      =%set $=^^^!$:^^^"=""^^^!%=                     inner delay   =%%$n1c%
%=      =%set $=^^^!$:^^^"=34#^^^!%$n1c%
%=      =%set $=^^^!$:42=4\2^^^!^&set $=^^^!$:\*=42^^^!%=            encode 42   =%%$n1c%
%=      =%set $=^^^!$:94#=9\4#^^^!^&set "$=^!$:^^^=94#^!"^&set $=^^^!$:33#=3\3#^^^!^&call set "$=%%^^^$:^^^!=33#%%"^^^!^&set $=^^^!$:34#=^^^"^^^!%$n1c%
%=      =%set $=^^^!$:3\4#=34#^^^!^&set $=^^^!$:63=6/3^^^!^&set $=^^^!$:?=63^^^!%=   replace 63-?   =%%$n1c%
%=      =%for /f "delims=" %%r in ("^!$^!") do (!forC_! (1,1,^^^!$eCount^^^!) do endlocal%$n1c%
%=         =%set ?=^^^!^&!forQ_! (%%r) do (setlocal enableDelayedExpansion%=   outer delay   =%%$n1c%
%=            =%set $=%%~?^&set $=^^^!$:42=*^^^!^&set $=^^^!$:4\2=42^^^!%=   decode 42   =%%$n1c%
%=            =%set $=^^^!$:63=?^^^!^&set $=^^^!$:6/3=63^^^!^&call set "$=%%^^^$:33#=33#^^^!%%"^^^!%$n1c%
%=            =%if defined ? (set "$=^!$:94#=^^^!"^&set $=^^^!$:33#=^^^!)else set "$=^!$:94#=^^^^^!"^&set "$=^!$:33#=^^^!"%$n1c%
%=            =%set $=^^^!$:9\4#=94#^^^!^&set $=^^^!$:3\3#=33#^^^!^&set $=^^^!$:""=^^^"^^^!%$n1c%
%=            =%!forLineR_! ("^!$^!") do endlocal^&set "%%r"%$n1c%
%=         =%)%$n1c%
%=      =%)%$n1c%

%=   =%)else setlocal enableDelayedExpansion^&set ?="
:: )
2>nul %macroEnd%
%endlocalR_% (
%$%
)
setlocal disableDelayedExpansion
:: (
   setlocal enableDelayedExpansion
   :: (
      set "$var0=simple"
      set "$var1=\*\*\*\*\*\*"
      %necho#$_% $var0, $var1
   :: )
   %endlocal_% $var0, $var0, $var1
   %necho_% $var0=%$var0%_
   %necho_% $var1=%$var1%_

   set "$var=This ^Wo" rks !"

   setlocal
   :: (
      setlocal enableDelayedExpansion    &%n2echo_% $var=!$var!_ &endlocal

      setlocal disableDelayedExpansion
      :: (
         set "$verify=failed"
      :: )
      %endlocal_% $var

      2>nul set "$verify" &&%endoftest%

      setlocal enableDelayedExpansion    &%necho_% $var=!$var!_ &endlocal
   ::)
   endlocal

   setlocal
   :: (
      setlocal enableDelayedExpansion    &%n2echo_% $var=!$var!_ &endlocal

      setlocal enableDelayedExpansion
      :: (
         set "$verify=failed"
      :: )
      %endlocal_% $var

      2>nul set "$verify" &&%endoftest%

      setlocal enableDelayedExpansion    &%necho_% $var=!$var!_ &endlocal
   ::)
   endlocal
::)
endlocal
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
!necho_!?=^^^!?^^^!_%$n1c%
!necho_!$=^^^!$^^^!_%$n1c%
!necho_!r=%%r_%$n1c%
:skip ()

cls
echo.¯ '%$iFile%'
type "%$iFile%" &echo.
echo.® '%$iFile%'

more "%$iFile%" > "tmp.TXT"

for /f %%? in ( '2^>nul ^( ^< "%$iFile%" find.EXE /c /v "" ^)' ) do set "$end=%%?"

< "tmp.TXT" (

       set "c=" &%forC_% (

              1, 1, %$end%

       ) do   set "$=" &set /p "$=" &(

              set /a c += 1
              setlocal enabledelayedexpansion
              rem (
                     set "$i!c!=!$!"
              rem )
              %endlocal_% $i!c!
       )

       setlocal enabledelayedexpansion
       rem (
              set "$=" &%forC_% (

                     1,1,!c!

              ) do   %forQ_% (

                     "i%%~!"

              ) do   set "$=!$!!$%%~?!!$lf!"

              (<nul set /p =!$:~0,-1!)
       rem )
       endlocal

) > "%$oFile%"
set /a $end -= 1

echo.
echo.¯ '%$oFile%'
type "%$oFile%" &echo.
echo.® '%$oFile%'

echo.linefeeds[%$end%]
pause
exit
I copy entire code from this post and pasted to make sure you won't have problems with removed TABS. It didn't break luckily. :roll:

This amount of code, I know that's insane, it is based upon dBenhams safe return technique which is defined in disabled expansion, has low-coupling and is therefor much shorter. After all I only needed my Endlocal_ macro :?

Post Reply