Convert CRLF to LF

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Convert CRLF to LF

#16 Post by Liviu » 11 Aug 2012 12:03

Ed Dyreen wrote:This amount of code, I know that's insane

Holy smoke! Don't know why you put all that Dutch inside "code" tags ;-)

Ed Dyreen wrote: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:

EDIT: Copies and runs fine now, thank you. To get the right graphic characters, the code should be pasted into an editor using the ANSI codepage.

Time for some studying now. I'll be back ;-)

Liviu

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

Re: Convert CRLF to LF

#17 Post by Ed Dyreen » 11 Aug 2012 12:34

'
I've edited previous page,

Shrunk down to only those macros that were actually in use.
I also add 'chcp 850' as that can cause problems too, but that's your field of expertise Liviu :)

I just recently learned I should document my code better.
Sorry for the poor documentation, :oops:

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

Re: Convert CRLF to LF

#18 Post by Ed Dyreen » 11 Aug 2012 13:42

'
I'd use this simple solution if I only could figure out why it eat a single linefeed after the ";Please <preserve=^"this line"!>" :(

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]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. . .
CRLFtoLF.CMD

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0

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


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=" &for /l %%! in (

              1, 1, %$end%

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

              set /a c += 1
              setlocal enabledelayedexpansion
              rem (
                     if %%~! neq %$end% set $=!$!!$lf!!$lf!^


                     <nul set /p =!$!
              rem )
              endlocal
       )

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

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

echo.linefeeds[%$end%]
pause
exit
I figured it has something to do with the caret, the set /p and the file redirection.

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

Re: Convert CRLF to LF

#19 Post by Liviu » 11 Aug 2012 15:03

Ed Dyreen wrote:'I'd use this simple solution if I only could figure out why it eat a single linefeed after the ";Please <preserve=^"this line"!>" :(

Know this doesn't help ;-) but under XP it's also losing the outer quotes on that line.

Liviu

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

Re: Convert CRLF to LF

#20 Post by Liviu » 13 Aug 2012 13:05

Ed Dyreen wrote:I didn't test it properly :oops: but it seem to work now :mrgreen:

Thinks it still fails if the file starts with a " quote (for example if the first line is literally "a"b), or has lines which contain * asterisks.

The following goes along a similar idea, but rather than collect the whole file, it counts empty lines and prepends them to the next not empty line. This also means that trailing empty lines at the end of the file are discarded - but those couldn't be output anyway until someone figures the incantation to output a single control character like LF. The code also breaks on lines with " quotes - think that's fixable, but haven't spent much time on it.

Code: Select all

@echo off
setlocal disableDelayedExpansion

if "%~1"=="" (set "$iFile=in.txt") else (set "$iFile=%~1")
if "%~2"=="" (set "$oFile=ou.txt") else (set "$oFile=%~2")
set $lf=^


::
for /f %%n in ('2^>nul ^( ^<"%$iFile%" find.exe /c /v "" ^)') do set "$end=%%n"
echo.linefeeds[%$end%]

<"%$iFile%" (
  set /a x = 0
  for /l %%n in (1, 1, %$end%) do (
    set "$="
    set /p "$="
    @rem setlocal enabledelayedexpansion
    @rem doesn't work under vista/win7
    @rem if defined $ (<nul set /p ="!$!!$lf!") else (<nul set /p "=!$lf!")
    @rem endlocal
    if defined $ (
      set "$$="
      setlocal enabledelayedexpansion
      for /l %%x in (1, 1, !x!) do (
        set "$$=!$$!!$lf!"
      )
      <nul set /p =!$$!!$!!$lf!
      endlocal
      set /a x = 0
    ) else (
      set /a x += 1
    )
  )
) >"%$oFile%"

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

Liviu

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

Re: Convert CRLF to LF

#21 Post by foxidrive » 13 Aug 2012 17:38

If your Windows os can run 16 bit com files then I use CRLF.com from

http://www.horstmuc.de/horst.htm


This is not a task that batch files is good at...

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

Re: Convert CRLF to LF

#22 Post by Ed Dyreen » 13 Aug 2012 21:29

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Liviu wrote:Thinks it still fails if the file starts with a " quote (for example if the first line is literally "a"b)
On XP that can be fixed.
Liviu wrote:or has lines which contain * asterisks.
That's actually a problem with the way how endlocal macro works.
( Cannot return strings that contain asterisks unless they are escaped like so '\*' )
The only technique I know that can escape and delay the asterisk uses a function call.

in.TXT

Code: Select all

"a"b
or has lines which contain * asterisks.
this
^wo rks!



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



This
^wo rks!
ou.TXT

Code: Select all

"a"b[0A]or has lines which contain * asterisks.[0A]this[0A]^wo rks![0A][0A][0A][0A]";Please <preserve=^"this line"!>"[0A]";Please <preserve=^[0A]";Please[0A][0A][0A][0A]This[0A]^wo rks!
CRLFtoLF.CMD

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_% 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 ()

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





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 )
              %forQ_% ("$i!c!") do %forLineR_% ("$i!c!=!%%~?!") do endlocal &set "%%r"
       )

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

                     1,1,!c!

              ) do   %forQ_% (

                     "i%%~!"

              ) do   set "$=!$!!$%%~?!!$lf!"
              (echo(!$:~0,-1!)>&2
              <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
Of course this is inefficient, if you expand the forQ_, forC_ and forLineR_ macro,
you get a normally short code that's better suited to be posted on a forum.
In the last loop I believe the code can be enhanced to go beyond the 8k limit in much the same way as Liviue demonstrates.

ED

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

Re: Convert CRLF to LF

#23 Post by Liviu » 14 Aug 2012 00:17

foxidrive wrote:This is not a task that batch files is good at...

Oh, obviously. This thread wandered quite far by now. Apologies to the OP (who hasn't followed up recently) for overloading on technicalities, hope he's not completely sca(r)red away ;-) That said, it's brought up some good points about "* (mis)handling, and xp/vista/7 differences.

Ed Dyreen wrote:if you expand the forQ_, forC_ and forLineR_ macro, you get a normally short code that's better suited to be posted on a forum

Thanks for the followup, though it may be a few days before I have a chance to look back at this more closely. Concur about shorter code ;-) and was actually close to posting an abbreviated version of your %endlocal_% but realized that while "flattening" the macros I must have misquoted some delayed-expansion flag, and it was doubling ^^ carets in the output. I'll post back when I have that cleared up.

Liviu

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

Re: Convert CRLF to LF

#24 Post by Ed Dyreen » 14 Aug 2012 04:39

Liviue wrote:Thanks for the followup, though it may be a few days before I have a chance to look back at this more closely. Concur about shorter code ;-) and was actually close to posting an abbreviated version of your %endlocal_% but realized that while "flattening" the macros I must have misquoted some delayed-expansion flag, and it was doubling ^^ carets in the output. I'll post back when I have that cleared up.
Liviu, you are just being lazy :mrgreen:

Removed the macro dependencies to make the code short and self-containing.
Integrated Liviue's solution to get over 8k limit.

This works on XP

in.TXT

Code: Select all



"a"b
or has lines which contain * asterisks.
this
^wo rks!



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



This
^wo rks!
ou.TXT

Code: Select all

[0A][0A]"a"b[0A]or has lines which contain * asterisks.[0A]this[0A]^wo rks![0A][0A][0A][0A]";Please <preserve=^"this line"!>"[0A]";Please <preserve=^[0A]";Please[0A][0A][0A][0A]This[0A]^wo rks!
CRLFtoLF.CMD

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0 &set $lf=^


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


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=" &for /l %%! in (

              1, 1, %$end%

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

              set /a c += 1
              setlocal enabledelayedexpansion
              rem (
                     set "$i!c!=!$!"
              rem )
              for %%? in ("$i!c!") do for /f ^"eol^=!$lf!delims^=^" %%r in ("!%%~?!") do endlocal &set "%%~?=%%r"
       )

       setlocal enabledelayedexpansion
       rem (
              set "$=" &for /l %%! in (

                     1,1,!c!

              ) do   for %%? in (

                     "i%%~!"

              ) do   if defined $%%~? (

                     if %%~! == !$end! (

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

                     <nul set /p ="!$!">&2
                     <nul set /p ="!$!"
                     set "$="

              ) else if %%~! neq !$end! (

                     <nul set /p =!$lf!>&2
                     <nul set /p =!$lf!
              )
       rem )
       endlocal

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

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

echo.linefeeds[%$end%]
pause
exit
There was another problem that would corrupted the output if the line started with a linefeed :!:


ED

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

Re: Convert CRLF to LF

#25 Post by Ed Dyreen » 14 Aug 2012 05:17

'
But then I found I could eliminate the push over endlocal all together :D

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0 &set $lf=^


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


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" (

       for /l %%! in (

              1, 1, %$end%

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

              setlocal enabledelayedexpansion
              rem (
                     if defined $ (

                            if %%~! neq !$end! set "$=!$!!$lf!"

                            <nul set /p ="!$!">&2
                            <nul set /p ="!$!"

                     ) else if %%~! neq !$end! (

                            <nul set /p =!$lf!>&2
                            <nul set /p =!$lf!
                     )
              rem )
              endlocal
       )

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

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

echo.linefeeds[%$end%]
pause
exit
On XP; 'set /p' strips any starting linefeed/s if they are quoted yet strips any linefeed/s unless they are quoted :!:


ED

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

Re: Convert CRLF to LF

#26 Post by Liviu » 14 Aug 2012 10:27

Ed Dyreen wrote:Liviu, you are just being lazy :mrgreen:

Why, of course. Laziness and impatience is what drives people to write short fast code ;-)

Ed Dyreen wrote:This works on XP

Don't have my XP machine handy today, so I'll take your word. But both your latest versions do drop empty lines on Windows 7. The issue was not with XP, (<nul set /p "=!$lf!") works there, too (http://www.dostips.com/forum/viewtopic.php?p=19003#p19003).

Liviu

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

Re: Convert CRLF to LF

#27 Post by Ed Dyreen » 14 Aug 2012 11:56

Liviu wrote:But both your latest versions do drop empty lines on Windows 7.
How about

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0 &set $lf=^


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


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" (

       for /l %%! in (

              1, 1, %$end%

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

              setlocal enabledelayedexpansion
              rem (
                     if defined $ (

                            if %%~! neq !$end! set "$=!$!!$lf!"

                            <nul set /p ="!$!">&2
                            <nul set /p ="!$!"

                     ) else if %%~! neq !$end! (

                            <nul set /p "=!$lf!">&2
                            <nul set /p "=!$lf!"
                     )
              rem )
              endlocal
       )

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

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

echo.linefeeds[%$end%]
pause
exit


ED

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

Re: Convert CRLF to LF

#28 Post by Liviu » 14 Aug 2012 14:54

Ed Dyreen wrote:
Liviu wrote:But both your latest versions do drop empty lines on Windows 7.
How about [...]

Just re-verified under win7 (pro x64 if it matters) and this one drops empty lines, too. In fact, it's pretty close to my first try. Don't know that there is an easy way around it on vista/win7 since they trim control characters at both ends. The logic would have to wait for the first line with at least 2 non-control characters, use all but one to flush the previously accumulated contents, then append what comes next to the remaining non-control until another such line is encountered. Possible, of course, but not pretty, and perhaps not worth the gymnastics.

My actual hope for this thread was that maybe an answer popped as to how one single control character could be sent to stdout, consistently between xp/vista/win7. I start to think that, if such a trick exists at all, it may not involve "set/p".

Liviu

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

Re: Convert CRLF to LF

#29 Post by Ed Dyreen » 14 Aug 2012 21:12

'
It happens a lot that different blocks of code are executed based upon the client OS.

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0 &set $lf=^


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

set /a XP = 512600 &for /f "tokens=2 delims=[]" %%? in (

       'ver.EXE'

) do  for /f "tokens=2-4 delims=. " %%a in (

       "%%~?"

) do  set /a $ver = %%~a%%~b%%~c

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" (

       for /l %%! in (

              1, 1, %$end%

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

              setlocal enabledelayedexpansion
              rem (
                     if defined $ (

                            if %%~! neq !$end! set "$=!$!!$lf!"

                            <nul set /p ="!$!">&2
                            <nul set /p ="!$!"

                     ) else if %%~! neq !$end! if !$ver! gtr !XP! (

                            <nul set /p ="!$lf!">&2
                            <nul set /p ="!$lf!"

                     ) else (

                            <nul set /p "=!$lf!">&2
                            <nul set /p "=!$lf!"
                     )
              rem )
              endlocal
       )

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

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

echo.linefeeds[%$end%]
pause
exit
I only test this on XP, it's not like I have a 7 laying around here. :?


ED

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

Re: Convert CRLF to LF

#30 Post by Squashman » 14 Aug 2012 22:11

Doesn't retain blank lines on Windows 7.

Post Reply