Windows Shell Script (or BAT file) to strip extra CR character

Hi Folks,

I have a file being produced by a program that has:
1 - some undesired characters at the start of the first line
2 - an extra CR character at the end of each line (each line terminates with CR + CR + LF)

I'm looking for either a BAT file or a windows shell script that can:
 - strip the undesired characters from the start of first line
 - strip one of the two CR characters from the end of each line

All lines will start with one or more numeric digits and should terminate with a single CR+LF combination.

It would be preferable if the existing file could be edited instead of reading and writing to a new file, although that's not strictly required.

Any takers?

Sample file attached!

Who is Participating?
Lee W, MVPConnect With a Mentor Technology and Business Process AdvisorCommented:
A simple VB script should be able to do the trick.

Option Explicit

Dim FSO, SourceFile, DestFile
Dim SourceFileName, DestFileName

SourceFileName = "C:\path\To\Source\File.txt"
DestFileName = "C:\path\To\Source\"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFile = FSO.OpenTextFile(SourceFileName, 1)
Set DestFile = FSO.CreateTextFile(DestFileName, True)

DestFile.Write Replace(SourceFile.ReadAll, vbCr + VbCrLF, VbCrLf)

Open in new window

RobSampsonConnect With a Mentor Commented:
Hi Matt,

I can't see any invalid characters on the first line, but see if this will replace your CR+CR+LF sequences with CR+LF


strOldFile = "Sample.csv"
strNewFile = "Sample2.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objOldFile = objFSO.OpenTextFile(strOldFile, ForReading, False)
Set objNewFile = objFSO.CreateTextFile(strNewFile, True)
While Not objOldFile.AtEndOfStream
	strLine = objOldFile.ReadLine
	objNewFile.WriteLine Replace(strLine, Chr(10) & Chr(10) & Chr(13), Chr(10) & Chr(13))

WScript.Echo "Done"

Open in new window

mattrattAuthor Commented:
Thanks for solutions! I actually like them both :-)

The invalid characters are non-printable and take up three bytes at the start of the file - but given they appear before the first comma I think I can live with them as-is.

I couldn't pick just one solution given you'd both posted almost instantaneously, hence the point split.  For the sake of the exercise I chose leew's solution - not because it's necessarily better than RobSampson's, I just like the elegance of a Write / Replace / ReadAll in the one line.

Sure, no problem. Thanks.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.