Solved

Problems with VB Script to remove CR/LF

Posted on 2011-03-08
6
719 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:moore315
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35074441
Your script only remove OD carriage return : OA newline are not removed.

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

oRegExp.Pattern = "("".*)(\r)(.*"")"
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 35075415
Okay, let's try a slightly different approach, see if this gives the results you want.

' 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, 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
0
 

Author Closing Comment

by:moore315
ID: 35131391
Works great, thanks.
0
Technology Partners: 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: 36093097
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
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36093297
No problem, we should be able to work around that, I'll poke at it later this evening.

~bp
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36096913
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
0

Featured Post

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!

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 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