Link to home
Start Free TrialLog in
Avatar of moore315
moore315

asked on

Problems with VB Script to remove CR/LF

Having trouble with a script that I received from EE (script is attached).  Worked in test but having problems 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).

Expert 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?
' 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

SL.Account.Export.csv
SL.Account.Export.noCRLF.csv
Avatar of Tuyau2poil
Tuyau2poil

Your script only remove OD carriage return : OA newline are not removed.

try to replace /n with /r in that line :

oRegExp.Pattern = "("".*)(\r)(.*"")"
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of moore315

ASKER

Works great, thanks.
Bill - you're gonna hate me but...

When I run this script with an input file with multiple lines there is a problem.  The input file has 5 rows.  It seems like the end of every row gets messed up (although it was every other row in a previous test).  Each row needs to end with 0D0A.  They end with 0D followed by ",".  

Each row starts with a long string in quote that starts with "003G000..." and each row ends with "end1" or "end2", etc. If I open the output file with excel it looks fine but if I open it with Notepad it is all one row.  Looking at it in a Hex editor I can see the rows ending in in 0D, then ",".


ContactExportTEST4NoHdr2.csv
ContactExportTEST4NoHdrNoLF2.csv
No problem, we should be able to work around that, I'll poke at it later this evening.

~bp
Okay, give this small change a try and see how it works on various test files you have.

This will preserve any [CR][LF] pairs as [CR][LF] which should be the normal end of line.  Then any single [LF] are replaced by a space, while any single [CR] are replaced by a [CR][LF] pair.

' 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

' Remove extra line breaks
sData = Replace(sData, vbCrLf, "<CRLF>")
sData = Replace(sData, vbCr, "<CRLF>")
sData = Replace(sData, vbLf, " ")
sData = Replace(sData, "<CRLF>", vbCrLf)

' 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