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