Solved

VBS to convert CR/LF to space

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

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 52

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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

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 52

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 52

Expert Comment

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

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

861 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

24 Experts available now in Live!

Get 1:1 Help Now