Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS Search and replace REGex

Status
Not open for further replies.

dswaff

IS-IT--Management
Aug 10, 2015
21
US
I need help with my script.
It works by dragging and dropping a file on to the vbs script.

When it does it need to search through the text file dropped on it and find the first char of every line.
If the first char is a 0 then replace 0 with double space.
If the first char is a 1 then replace with a page break.

I only have it searching for the 0 at the beginning of the file currently.
It always errors out when trying to find the file that was dropped on to it.

Any help is appreciated.


Here is the script:

dim myfile
Set fso = CreateObject("Scripting.FileSystemObject")
Set regEx = New RegExp
regEx.Pattern = "^(0)"
regEx.Global = True
myfile = fso.GetFile(WScript.Arguments(0))
myfile = chr(34) & myfile & chr(34)
Do While Not EOF
txtFile = regEx.Replace(fso_OpenTextFile(WScript.Arguments(0)).ReadAll, " ")
fso_OpenTextFile(WScript.Arguments(0), 2, True).Write txtFile
loop
fso.close
 
Not tested, but something like this framework should work. I removed all the regex / search and replace, you should be able to piece that in. It processes each line in the file, appends to txtOutput, then overwrites the file at the end (again, not tested).

>It always errors out when trying to find the file that was dropped on to it.
But you didn't tell us the error message.

Code:
Const ForReading = 1, ForWriting = 2, ForAppending = 8

myfile = Wscript.Arguments(0)

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(myfile, ForReading)

Do Until f.AtEndOfStream
   txtLine = f.ReadLine
   [COLOR=#4E9A06]'If the first char is a 0 then replace 0 with double space.
   'If the first char is a 1 then replace with a page break.[/color]
   txtOutput = txtOutput & txtLine & vbCrLf
Loop

f.Close
Set f = fso.OpenTextFile(txtOutput, ForWriting, True)
f.Write txtOutput
f.Close
 
>It processes each line in the file

Which means that you are missing out on pretty much one of the main points of regular expressions - that they can do multiple searches against large blocks of text. Sure, a perceived problem might be how can you do different replacements depending on what you have found. With vbscript regular expressions this might most easily handled by doing two passes, one for the 0 and one for the 1.

Or we could leverage a very little known feature that I illustrated in thread222-438528.

Here's a VBscript variant of that example that illustrates how we might deal with your scenario (my string source is just a string, but can simply be replaced by an fso ReadAll)

Code:
[blue]Dim myReplacer
dim strSource

Set myReplacer = New reHelper
strSource="0 spoons" & vbcrlf & "1 knife" & vbcrlf & "7 hats with 1 tie"
With new RegExp 're
	.Pattern = "^(0|1)"
	.MultiLine = True
	.Global = True
	MsgBox .Replace(strSource, myReplacer)
End With
	
Class reHelper
    Public Default Function ReplacerFunction(a,b,c,d)
        Select Case a
            Case "0"
                ReplacerFunction = "Nought"
            Case "1"
                ReplacerFunction = "One"
            Case Else
                ReplacerFunction = a
        End Select       
    End Function
End Class[/blue]
 
>which means that you are missing out on pretty much one of the main points of regular expressions
YES TRUE! Some day I will just bear down and learn regular expressions... thats clearly a better, less complicated... and faster and more scalable solution. Some day.
 
You guys have been great.
StrongGM thank you for your example.
The one piece I was missing was regex.multiline.

The only problem I have now is inserting a page break.
If I use regEx.Replace(fso_OpenTextFile(WScript.Arguments(0)).ReadAll, "ch(12)")
instead of inserting the page break it literally inserts ch(12) as text.

Any ideas?





So my code looks like this now:


Set fso = CreateObject("Scripting.FileSystemObject")
Set regEx = New RegExp
regEx.Pattern = "^(0)"
regEx.Global = True
regEx.MultiLine = True

txtFile = regEx.Replace(fso_OpenTextFile(WScript.Arguments(0)).ReadAll, " ")
fso_OpenTextFile(WScript.Arguments(0), 2, True).Write txtFile

regEx.Pattern = "^(1)"
regEx.Global = True
regEx.MultiLine = True

txtFile = regEx.Replace(fso_OpenTextFile(WScript.Arguments(0)).ReadAll, "ch(12)")
fso_OpenTextFile(WScript.Arguments(0), 2, True).Write txtFile
 
So you are doing two passes; the code example I provided allows you to do it in a single pass.

As for the page break:

"ch(12)" => chr(12)
 
Ok. So I think I need to set encoding on the file or something. Its just a text file.
I tried chr(12) but it just shows chr(12) in notepad++ and word. No actual page break.
 
Tried that. I just get error line 23.

What Found I could do though, since writing it in notepad++, I can hold alt, type 012 release alt and it inserts Page break FF. I can then copy the pagebreak FF and paste it in quotes and it works !

I also now have it saving as DOC and I am working on setting it to landscape and setting the margins.
I have it saving and encoding as a word doc. Just orentation is currently not working.
 
IM back.

I keep running into EOF errors.
I have tried diffrent ways, but it keeps messing up my file format.

Any ideas.


Dim mytextfile
Dim newtext

Set fso = CreateObject("Scripting.FileSystemObject")





Dim myReplacer
dim strSource

Set myReplacer = New reHelper
strSource = ReadEntireFile
With new RegExp 're
.Pattern = "^(0|1)"
.MultiLine = True
.Global = True
newtext = .Replace (strSource, myReplacer)
End With

fso_OpenTextFile(WScript.Arguments(0), 2, True).Write newtext



Class reHelper
Public Default Function ReplacerFunction(a,b,c,d)
Select Case a
Case "0"
ReplacerFunction = vbCRLF & " "
Case "1"
ReplacerFunction = " "
Case Else
ReplacerFunction = a
End Select
End Function
End Class
 
Sorry. That was left overs from what I was trying using a line by line read till end of stream. It got messy though.
Anyway, ignore that.

The line is question is strSource = fso_OpenTextFile(WScript.Arguments(0)).ReadAll
Error: input past end of file
code 800a003e


Here is the original:

Dim mytextfile
Dim newtext

Set fso = CreateObject("Scripting.FileSystemObject")

strSource = fso_OpenTextFile(WScript.Arguments(0)).ReadAll



Dim myReplacer
dim strSource

Set myReplacer = New reHelper
With new RegExp 're
.Pattern = "^(0|1)"
.MultiLine = True
.Global = True
newtext = .Replace (strSource, myReplacer)
End With

fso_OpenTextFile(WScript.Arguments(0), 2, True).Write newtext



Class reHelper
Public Default Function ReplacerFunction(a,b,c,d)
Select Case a
Case "0"
ReplacerFunction = vbCRLF & " "
Case "1"
ReplacerFunction = " "
Case Else
ReplacerFunction = a
End Select
End Function
End Class
 
Drag and drop the txt file on to the script vbs.
via: WScript.Arguments(0)
 
Well, that would suggest that the text file you are dropping is empty ...
 
Well the files are not empty. I have a 32kb and an 18kb file I am testing with that has data.
These files I receive I have no control over. I just have to make them work.

I did notice when I write back to the file is when I have a problem.
Keep getting invalid procedure call on this line:
fso_OpenTextFile(WScript.Arguments(0), 2, True).Write (newtext)


I am wondering when I attempt to write the file back if its still reading. Because after the function finishes the file turns empty to 0kb.

I am stummped and open to ideas.
 
>I did notice when I write back to the file is when I have a problem.
I thought you said the problem was with reading the file?

Well anyhow, just a guess but it could also be a problem with the characters in file you are reading (see the link in my reply to thread329-1755526). That's just a guess though.

 
Thank you very much for the example.

Here is what I found.
After reading the file I wasn't closing the file.

Then I was trying to write back to the same file using fso_OpenTextFile(WScript.Arguments(0), 2, True,0).Write txtFile

I believe that because I was not closing the file after reading it, thats why I was getting the EOF errors.

And I belive my write function was failing on certain files because they were malformed. So to correct that I used the fso.createTextFile function instead of Openning the text file for writing.

Thanks again.
That was driving me nutty.
Here is the final code:





Dim mytextfile
Dim newtext
dim fso
dim objFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set objFile = fso_OpenTextFile(WScript.Arguments(0), 1,true)
strSource = objFile.ReadAll
objFile.Close




Dim myReplacer
dim strSource

Set myReplacer = New reHelper
With new RegExp 're
.Pattern = "^(0|1)"
.MultiLine = True
.Global = True
newtext = .Replace (strSource, myReplacer)
End With

Set objFile = fso.createTextFile(WScript.Arguments(0), 2,true)
objFile.WriteLine newtext
objFile.Close



Class reHelper
Public Default Function ReplacerFunction(a,b,c,d)
Select Case a
Case "0"
ReplacerFunction = vbCRLF & " "
Case "1"
ReplacerFunction = " "
Case Else
ReplacerFunction = a
End Select
End Function
End Class

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top