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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search and Replace multiple strings at once? 1

Status
Not open for further replies.

ShopMonkey

Technical User
Nov 19, 2007
5
US
I'm working with some VERY large ascii lpd data files and it turns out there are some non-printing character sequences that are causing problems for my print management software. I need to do some heavy search and replace functions on the data for it to work properly.

I found a website with some variations on reading large files ( ) and the technet "Scripting Guy" article on searching and replacing text in a file ( )

Since the files I'm working with can be extremely large (80mb+ ) I chose to use the "read byte" method I found on the first reference site and the following is the code I came up with after combining the two:

const ForReading = 1
Const ForWriting = 2
const TristateFalse = 0
dim strDataCapture
dim objFS
dim objFile
dim objTS
set objFS = CreateObject("Scripting.FileSystemObject")
set objFile = objFS.GetFile(PW_GetJobFilename)
set objTS = objFile.OpenAsTextStream(ForReading)

strDataCapture = objTS.Read(objFile.Size)
objfile.close

Do while strDataCapture.AtEndOfFile <>true

strDataCapture.Replace("\x00","\x20")
strDataCapture.Replace("\x0d""\x0a""\x01","\x0d""\x0a""\x31")
strDataCapture.Replace("\x0d""\x0a""\x09","\x0d""\x0a""\x20")
strDataCapture.Replace("\x0d""\x0a""\x0b","\x0d""\x0a""\x20")
strDataCapture.Replace("\x0d""\x0a""\x11","\x0d""\x0a""\x30")
strDataCapture.Replace("\x0d""\x0a""\x13","\x0d""\x0a""\x30")
strDataCapture.Replace("\x0d""\x0a""\x19","\x0d""\x0a""\x2d")
strDataCapture.Replace("\x0d""\x0a""\x1b","\x0d""\x0a""\x2d")
strDataCapture.Replace("\x0d""\x0a""\x2a","\x0d""\x0a""\x20""\x2a")
strDataCapture.Replace("\x0d""\x0a""\x69","\x0d""\x0a""\x31")
strDataCapture.Replace("\x0d""\x0a""\x8b","\x0d""\x0a""\x31")
strDataCapture.Replace("\x0d""\x0a""\x8d","\x0d""\x0a""\x20")
strDataCapture.Replace("\x0d""\x0a""\x8f","\x0d""\x0a""\x20")
strDataCapture.Replace("\x0d""\x0a""\xbb","\x0d""\x0a""\x31")
strDataCapture.Replace("\x0d""\x0c","\x0d""\x0a""\x31")
strDataCapture.Replace("\x9e","\x4f")
strDataCapture.Replace("\xa6","\x1b")

loop

Set objFile = objFS.OpenTextFile(PW_GetJobFileName, ForWriting)

objFile.Write strDataCapture
objFile.Close

As you can see, I have a LOT of strings to search and replace.

I'm not sure if I have the replace loop correct, or if it's even necessary with that command. I'm also uncertain as to how to properly write the whole mess back to the file properly.

All the code above is mish-mashed together from a lot of different sites with my very limited understanding and I'm hoping I at least got some of it right.

I appreciate all the help and insight I've received from this forum, both in answers received and other posts I've read.
ShopMonkey
 
Look into regular expressions. Here is a thread that may be of some help.

thread222-1194501

Swi
 
I think I understand.
The problem I'm having is that I have to replace those invalid characters with a variety of other characters, and I'm not sure how to integrate the three parts of the code together.

I know I want something like this to read the file (it's supposed to be fairly fast):[/color red]
--
const ForReading = 1
const TristateFalse = 0
dim strSearchThis
dim objFS
dim objFile
dim objTS
set objFS = Server.CreateObject("Scripting.FileSystemObject")
set objFile = objFS.GetFile(PW_GetJobFilename)
set objTS = objFile.OpenAsTextStream(ForReading, TristateFalse)

strSearchThis = objTS.Read(objFile.Size)
----

And I guess I need something like this to do a search and replace:
[/color red]
----
dim re as object
set re = creatobject("vbscript.regexp")
with re
.global = true
.pattern = "[^\x20-\x7E]"
TrimAll = .replace(TrimAll, " ")
end with
----

and somehow end up with something like this, only faster?
[/color red]
----
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(PW_PW_GetJobFileName, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim ", "James ")

Set objFile = objFSO.OpenTextFile(PW_GetJobFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close
----

this last method does work, but it takes nearly a half hour to process an 80mb ascii text file, and I can't get it to do ALL the character replacements I need. Unfortunately, I cant just specify a range of hex codes, since channel-skip data requires certain characters in certain places.

these are the characters I need to search and replace:

(search, replace)
\xa6, \x1b
\x00, \x20
\x09, \x20
\x0b, \x20
\x2a, \x20\x2a
\x8d, \x20
\x8f, \x20
\x19, \x2d
\x1b, \x2d
\x11, \x30
\x13, \x30
\x01, \x31
\x69, \x31
\x8b, \x31
\xbb, \x31
\x0d\x0c, \x31
\x9e, \x4f
[/color red]

I will admit I have never worked with VB prior to my 1 previous question on these forums from last week.
 
Ok.. I think I got it compiled down correctly to this:

Code:
const ForReading = 1
const TristateFalse = 0
dim strSearchThis
dim objFS
dim objFile
dim objTS
set objFS = CreateObject("Scripting.FileSystemObject")
set objFile = objFS.GetFile(PW_GetJobFilename)
set objTS = objFile.OpenAsTextStream(ForReading, TristateFalse)

strSearchThis = objTS.Read(objFile.Size)
objTS.close

set re = New RegExp
with re
    .global = true
    .pattern = "\x00"
    strSearchThis = .replace(strSearchThis, "\x20")

    .pattern = "\xa6"
    strSearchThis = .replace(strSearchThis, "\x1b")

    .pattern = "\x9e"
    strSearchThis = .replace(strSearchThis, "\x4f")

    .pattern = "\x0d\x0a\x8d,\x0d\x0a\x09,\x0d\x0a\x0b,\x0d\x0a\x8f"
    strSearchThis = .replace(strSearchThis, "\x0d\x0a\x20")

    .pattern = "\x0d\x0a\x2a"
    strSearchThis = .replace(strSearchThis, "\x0d\x0a\x20\x2a")

    .pattern = "\x0d\x0a\x19,\x0d\x0a\x1b"
    strSearchThis = .replace(strSearchThis, "\x0d\x0a\x2d")

    .pattern = "\x0d\x0a\x11,\x0d\x0a\x13"
    strSearchThis = .replace(strSearchThis, "\x0d\x0a\x30")

    .pattern = "\x0d\x0a\x01,\x0d\x0a\x69,\x0d\x0a\x8b,\x0d\x0a\xbb,"
    strSearchThis = .replace(strSearchThis, "\x0d\x0a\x31")


end with

set objTS = objFile.OpenAsTextStream(ForWriting,TristateFalse)
objTS.Write strSearchThis 
objTS.Close

For some reason though, I get an error on this line still though: set objTS = objFile.OpenAsTextStream(ForWriting,TristateFalse)
I think the line is correct, at least from what I understood of the references online. If I change the "ForWriting, TristateFalse" to 2,0 for numeric reference, it doesn't error out, but I get a 0 byte file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top