Solved

VBS to convert CR/LF to space

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

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 51

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
 

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 51

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 51

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 51

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 51

Expert Comment

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

~bp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

12 Experts available now in Live!

Get 1:1 Help Now