Search code examples
cmdeol

change eol character via cmd


i have a file file1in with unix eol, i have a script to do some editing in it, but the editing is done into output.txt and is renamed as file1and this changes the eol to windows/dos

the code is given

set uu=file1
set vv=file2

setlocal EnableDelayedExpansion
(for /F "delims=" %%a in (%uu%) do (
   set "line=%%a"
   if "!line:345=!" neq "!line!" (
      if "!line:123=!" neq "!line!" (
         if not defined flag (
            findstr "123" %vv% | findstr "345"
            set flag=true
         )
      ) else (
         echo !line!
      )
   ) else (
      echo !line!
   )
)) >output.txt 
del %uu% 
rename output.txt file1

any way to change it back or retain unix eol via cmd without user input?

have tried directly inputing into the file1, gives a 0 kb file tried type output.txt>file1 gave dos/win eol echoing anything other than a blank line echo.>file1 changed the eol char


Solution

  • Here's a solution:

    @echo off
    ::Syntax: batchfile "file.txt" >"file2.txt"
    
    :init
    for /f %%c in ('copy /z "%~dpnx0" nul') do set cr=%%c
    (set lf=^
    
    )
    del file.tmp 2>nul
    for /f "delims=" %%i in ('findstr /n "^" "%~1"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!!lf!"
    echo(!line!
    endlocal
    )>>file.tmp
    setlocal enabledelayedexpansion
    findstr /v "!cr!!lf!" file.tmp
    endlocal
    del file.tmp 2>nul