Solved

VBS to convert CR/LF to space

Posted on 2011-03-01
12
1,856 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 56

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 56

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 56

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 56

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 56

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 56

Expert Comment

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

~bp
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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