computing
  • 3

Solved Removing Carriage Returns From a Textfile

  • 3

Hello,

I wrote a program that creates a textfile to be imported by a third party. I need to deploy my program to several machines and the problem I’m having is that the third party rejects my textfile because there are carriage returns and line feeds (0D 0A) at the end of every line. With all the stuff I’ve read, this appears to be the Windows standard. What I need to do is write a batch file that my program will call to strip out the carriage returns (0D) from all lines in my textflie. Because I need to deploy this to several machines and don’t want to install extra software on all these machines to accomplish this, is there a command in DOS that will allow me to do this?

Thanks.

Share

1 Answer

  1. It’s *almost* possible, but lines that start with “=” must be changed(I added a space before the “=” in the below script).

    Replace “oldfile” and “newfile” to your filenames:

    @echo off
    SetLocal DisableDelayedExpansion
    for /f "tokens=*" %%a in ('find /n /v "" ^< "oldfile"') do (
        set line=%%a
        SetLocal EnableDelayedExpansion
        set line=!line:*]=!
    
        rem "set /p" won't take "=" at the start of a line....
        if "!line:~0,1!"=="=" set line= !line!
        
        rem there must be a blank line after "set /p"
        rem and "<nul" must be at the start of the line
        set /p =!line!^
    
    <nul
        endlocal
    ) >> "newfile"
    pause
    

    • 1