Solved

VBS to convert CR/LF to space

Posted on 2011-03-01
12
1,754 Views
Last Modified: 2012-05-11
I have a text file being passed to another system.  Some addresses in the file have embedded carriage returns or line feeds which cause errors when trying to import to the receiving system.

Need a script that will convert CR (ASCII 13) or LF (ASCII 10) to space (ASCII 20).
0
Comment
Question by:moore315
  • 6
  • 6
12 Comments
 

Author Comment

by:moore315
ID: 35009665
It just occurred to me that this is more complicated than I originally wrote - problem is that each line is supposed to end with CR/LF (0D 0A).  The file has all fields embedded in quotes (22).  I need to remove only those CR/LF's that are embedded in quotes, not the ones at the legitimate end of the record.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35009693
This should do the job.  Save as a VBS file, and then run with cscript, and pass it either one file name, or two.  If one name passed it will be overwritten with the changes, if two it will read the first file, and write a changed version to the second file name.

You may only want a subset of these replaces:

sData = Replace(sData, vbCRLF, " ")
sData = Replace(sData, vbCR, " ")
sData = Replace(sData, vbLF, " ")

depending on exactly what you are trying to accomplish.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

' Replace desired strings
sData = Replace(sData, vbCRLF, " ")
sData = Replace(sData, vbCR, " ")
sData = Replace(sData, vbLF, " ")

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
MsgBox "Conversion done."
Wscript.Quit

Open in new window

~bp
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35009721
Oops, you changed the rules while I was typing.  Sounds like we may need to use a regular expression to do that, or a different technique.  Can you provide a sample data file for testing?

~bp
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:moore315
ID: 35010109
Yeah, sorry about that - the light bulb went on a little slowly...

The attached file has 10 rows (the column headings, then 9 rows of data).  Data rows, 3, 6, 7, 8 all have embedded CR/LF's in them.

 
SL.Account.CRLF.Test.csv
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35010189
Will all lines always start with "AC", or is that just in this small sample?

~bp
0
 

Author Comment

by:moore315
ID: 35011091
I need the script to run against multiple files.  In each file, every record will start with a code that begins with a two letter prefix followed by a dash.  It was "AC-" in the sample file I sent.  In other files it will be "CA-", or "SS-", etc.

0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 35012255
Okay, give this adjustment a try, should do what you want I think.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

' Replace desired strings
Set oRegExp = New RegExp
oRegExp.Global = True
oRegExp.IgnoreCase = False
oRegExp.Pattern = "("".*)(\n)(.*"")"
sData = oRegExp.Replace(sData, "$1 $3")

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
MsgBox "Conversion done."
Wscript.Quit

Open in new window

~bp
0
 

Author Closing Comment

by:moore315
ID: 35012940
Fantastic!  Thanks so much for your help.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35012999
Welcome, glad that worked out.

~bp
0
 

Author Comment

by:moore315
ID: 35069820
Having trouble with the script using a file from production (see attached).  Line 18 (company name starts with R4) and line 22 (starts with Coastal) have CR's in the street address and they are not getting removed (output file also attached).

You had asked if every record will begin with "AC-".  I don't see that anywhere in the code.  Is it using that approach somehow or not relying on the text at the beginning of each line?
SL.Account.Export.csv
0
 

Author Comment

by:moore315
ID: 35069829
output file attached here...
SL.Account.Export.noCRLF.csv
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35075495
Answer to follow on question posted in related question.

~bp
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

17 Experts available now in Live!

Get 1:1 Help Now