Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBS to convert CR/LF to space

Posted on 2011-03-01
12
Medium Priority
?
1,899 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 57

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 57

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 57

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 57

Accepted Solution

by:
Bill Prew earned 2000 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 57

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 57

Expert Comment

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

~bp
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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