• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 802
  • Last Modified:

vbs replace text2

Hi Experts,
Still in trouble here. See previous post 'vbs replace text'.
I am now having difficulty in getting rid of carriage returns/line feeds. I tried Chr(10) & Chr(13) and vbCRLF and nothing seems to work.
Glad to accept any help.
0
bravotango
Asked:
bravotango
  • 3
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
did you try to check if there are indeed carriage returns + line feed in the data?
with a hex editor (I assume a text file) this will be easily spottable...

you might try to replace first chr(10) & chr(13), and then chr(10) and chr(13) individually
0
 
bravotangoAuthor Commented:
Hi angelIII , yes I did that too and also tried "\r\n" and individually. Maybe it is not possible to find these with vbs.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
can you post the code that you are using, please
it should be possible.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
bravotangoAuthor Commented:
OK here it is again. It works fine with most search patterns and with "\^" which was another solution from a nice expert.
I think I can do it another way and not use vbCRLF as a replacement but I would like to know if I can search this text file for line feeds.
Dim pattern
Dim replacement
' We must define ForWriting because the
' imode constants are unknown to VBScript
Const ForWriting = 2
Const inF = "NewSavings.qif"
Const outF = "Testfile1.txt"

' Here are the strings for replacement.
pattern = "LDEBIT"
replacement = vbCRLF

Dim Text
Dim fso                 ' Object variable
Dim oFileIn, oFileOut   ' Text stream
Dim path, fileIn, fileOut

path = GetPath()        ' Retrieve current path to script.
fileIn = path & inF     ' Create filenames.
fileOut = path & outF

Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FileExists(fileIn) Then  ' Input file exists?
    WScript.Echo "File '" & fileIn & "' not found"
    WScript.Quit 1
End If

' Input file present; open file and create output file.
Set oFileIn = fso.OpenTextFile(fileIn)   ' Open input file.
Set oFileOut = fso.OpenTextFile(fileOut, _
                    ForWriting, True)    ' Open output file.

Do While Not (oFileIn.atEndOfStream)
    Text = oFileIn.ReadLine              ' Read a line.
    Text = Filter(Text, pattern, replacement)
    oFileOut.WriteLine Text              ' Write text.
Loop

WScript.Echo "Text file: " & fileIn & vbCrLf & _
             "Written into: " & fileOut

Function GetPath
    ' Retrieve the script path.
    Dim path
    path = WScript.ScriptFullName  ' Script name
    GetPath = Left(path, InStrRev(path, "\"))
End Function

Function Filter(txt, expr1, expr2)
    ' Replace expr1 with expr2 in text.
    Dim oReg

    Set oReg = New RegExp        ' Create regular expression.
    oReg.Global = True           ' All matches
    oReg.IgnoreCase = True       ' Make case-insensitive.

    ' Replace all expr1 with expr2.
    oReg.Pattern = expr1          ' Set pattern.
    Filter = oReg.Replace(txt, expr2)
End Function
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Your problem is in this line:

    oFileOut.WriteLine Text              ' Write text.

The WriteLine() method writes the value of "Text" to the file followed by vbCrLf.  That is what the "Line" part of "WriteLine" means.

If the files are not huge I would read the whole thing in at once using ReadAll(), do your replacement, and then write the whole thing back out.
0
 
bravotangoAuthor Commented:
Hi Idle Mind,

That fixed it. I wasn't aware that I could do that. You helped me in more ways than you would think. Now my project is finished I can charge off to the gym feeling much better! Finished script is this:

Const ForWriting = 2
Const inF = "NewSavings.qif"
Const outF = "Testfile1.txt"

Dim part2
Dim fso                 ' Object variable
Dim oFileIn, oFileOut   ' Text stream
Dim path, fileIn, fileOut

path = GetPath()        ' Retrieve current path to script.
fileIn = path & inF     ' Create filenames.
fileOut = path & outF

Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FileExists(fileIn) Then  ' Input file exists?
    WScript.Echo "File '" & fileIn & "' not found"
    WScript.Quit 1
End If

' Input file present; open file and create output file.
Set oFileIn = fso.OpenTextFile(fileIn)   ' Open input file.
Set oFileOut = fso.OpenTextFile(fileOut, _
                    ForWriting, True)    ' Open output file.

part2 = oFileIn.ReadAll
            part2 = StrReplace(part2, "!Type:Bank", "")
            part2 = StrReplace(part2, "LDEBIT", "")
            part2 = StrReplace(part2, "LDEP", "")
            part2 = StrReplace(part2, "ABWDL", "")
            part2 = StrReplace(part2, vbCRLF & "D", vbCRLF)
            part2 = StrReplace(part2, vbCRLF & "T", vbCRLF)
            part2 = StrReplace(part2, vbCRLF & "P", vbCRLF)
            part2 = StrReplace(part2, vbCRLF & "\^", vbCRLF)
            part2 = StrReplace(part2, vbCRLF, vbTAB)
            part2 = StrReplace(part2, vbTAB & vbTAB & vbTAB, vbCRLF)
    oFileOut.Write part2

WScript.Echo "Text file: " & fileIn & vbCrLf & _
             "Written into: " & fileOut

Function GetPath
    ' Retrieve the script path.
    Dim path
    path = WScript.ScriptFullName  ' Script name
    GetPath = Left(path, InStrRev(path, "\"))
End Function

Function StrReplace(txt, expr1, expr2)
    ' Replace expr1 with expr2 in text.
    Dim oReg

    Set oReg = New RegExp        ' Create regular expression.
    oReg.Global = True           ' All matches
    oReg.IgnoreCase = True       ' Make case-insensitive.

    ' Replace all expr1 with expr2.
    oReg.Pattern = expr1          ' Set pattern.
    StrReplace = oReg.Replace(txt, expr2)
End Function
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now