bravotango
asked on
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.
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.
ASKER
Hi angelIII , yes I did that too and also tried "\r\n" and individually. Maybe it is not possible to find these with vbs.
can you post the code that you are using, please
it should be possible.
it should be possible.
ASKER
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.Fi leSystemOb ject")
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
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.Fi
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Fi leSystemOb ject")
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
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.Fi
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
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