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

Posted on 2011-10-19
Last Modified: 2012-05-12
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!

Question by:mattratt
    LVL 65

    Assisted Solution

    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

    LVL 95

    Accepted Solution

    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


    Author Closing Comment

    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.

    LVL 65

    Expert Comment

    Sure, no problem. Thanks.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    I recently purchased an HP EliteBook 2540p notebook/laptop. It has two video ports on it – VGA and DisplayPort. HP offers an optional docking station for the 2540p that also has both a VGA port and a DisplayPort. There are numerous online reports do…
    Article by: Lee
    Windows 7 Ultimate and Enterprise (and 2008 R2) introduced a new feature you may not be aware of - Boot from VHD.   Boot from VHD (or what Microsoft refers to asNative Boot allows you to install Windows to a VHD (Virtual Hard Disk) file that is t…
    In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
    The viewer will learn how to back up in Windows 7 with native tools. This should be the first step. Third party tools should also be used. Access the Backup and Restore options: Click on the windows 7 start ball in the lower left corner of the scree…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now