Solved Removing carriage returns from a textfile

September 16, 2011 at 09:45:49
Specs: Windows XP
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.


See More: Removing carriage returns from a textfile

Report •

✔ Best Answer
September 16, 2011 at 16:49:16
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
September 16, 2011 at 13:19:10
Might open it in notepad and then save it. Notepad ought to strip that off.

1/3 of highway deaths are caused by drunks. The rest are by people who can't drive any better than a drunk.


Report •

#2
September 16, 2011 at 13:57:22
Thanks for the post. That did not seem to do anything however. I've read some other posts to create a batch file with the following code:

for /f "tokens=* delims=" %i in (abc.txt) do (
echo %i >> abc.txt
)

I'm using Windows XP and all this seemed to do was add a space after my line:

Note: 0D = carriage return
0A = line feed

Original line: Hello World!0D0A
After running batch file: Hello World! 0D0A
What I really want is: Hello World!0A


Report •

#3
September 16, 2011 at 16:46:51
Post your question in the proper forum here for more help: http://www.computing.net/forum/prog...

Report •

Related Solutions

#4
September 16, 2011 at 16:49:16
✔ Best Answer
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


Report •

#5
Report •

#6
September 19, 2011 at 08:24:39
Judago,

Thanks for the reply. When I try your code, I'm getting this error:
%%a was unexpected at this time.

When I remove one of the "%" so it reads %a, then I get some data in my new text file that removes the CR but it prints: !line!

...instead of my "Hello World"

Thanks.


Report •

#7
September 19, 2011 at 12:41:42
It's meant to be run as a .bat script, not be run individually from the command line.

The differences in parsing cause the problem you mentioned.


Report •

#8
September 27, 2011 at 07:57:59
Thanks Judago, that did the trick!

Report •

Ask Question