Solved

Problems with VB Script to remove CR/LF

Posted on 2011-03-08
6
714 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 51

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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 51

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 51

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

18 Experts available now in Live!

Get 1:1 Help Now