?
Solved

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

Posted on 2011-10-19
4
Medium Priority
?
380 Views
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!

Cheers,
Matt
sample.csv
0
Comment
Question by:mattratt
  • 2
4 Comments
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 1000 total points
ID: 36997023
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

Regards,

Rob.
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))
Wend
objOldFile.Close
objNewFile.Close

WScript.Echo "Done"

Open in new window

0
 
LVL 97

Accepted Solution

by:
Lee W, MVP earned 1000 total points
ID: 36997024
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\File.new.txt"

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

0
 

Author Closing Comment

by:mattratt
ID: 36997058
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.

Thanks!!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36997236
Sure, no problem. Thanks.

Rob.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 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