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.
bravotangoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
can you post the code that you are using, please
it should be possible.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.