I have vscript code that uses a regular expression to do a find and replace in a text file. The code works perfectly on smaller files. However, when searching a very large file (100mb), the find and replace never completes. I have tried ReadAll, ReadLine and now Read Bytes in attempt to speed up the search and replace process. I am reposting this question due to no resolution the first time around. I am way past due on getting this code to work. I am assigning the maximum points for this question. At this time, I could really use a solution as opposed to a suggestion. Your response would be greatly appreciated. Thank you for your time!
Diammond
strserver = "."
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const TristateFalse = 0
Const OverwriteExisting = True
dateStamp = Date()
timeStamp = Time()
'CREATE DICTIONARY OBJECT FOR PHONE-NUMBER-LIST.TXT AND READ THE FILE
set objList = CreateObject("Scripting.Di
ctionary")
Set objFSO = CreateObject("Scripting.Fi
leSystemOb
ject")
Set objFile = objFSO.OpenTextFile("c:\Ph
one-Number
-List.txt"
, ForReading)
strSearchString = objFile.ReadAll
objFile.Close
'CREATE REGULAR EXPRESSION TO SEARCH FOR PHONE NUMBERS
'THEN COMPARE PHONE NUMBERS FOUND IN DATAFILE.TXT WITH PHONE NUMBERS IN Phone-Number-List.txt
set re = New RegExp
re.global = true
re.pattern = "#Phone" & vbCRLF & Chr(34) & "\d{3}\d{3}\d{4}" & Chr(34)
Set objMatches = re.Execute(strSearchString
)
For Each objMatch in objMatches
objList.Add objMatch.Value, ""
Next
dim objTS
Set objFile = objFSO.GetFile("C:\datafil
e.txt)
Set objTS = objFile.OpenAsTextStream(F
orReading,
TriStateFalse)
strSearchString = objTS.Read(objFile.Size)
Set objMatches = re.Execute(strSearchString
)
objFSO.CopyFile "c:\REPLACED#s.LOG", "c:\REPLACED#s.bak", OverwriteExisting
Set objFile = objFSO.OpenTextFile ("c:\REPLACED#s.LOG", ForWriting)
objFile.Write ""
objFile.Close
icount = 0
For Each objMatch in objMatches
If Not objList.Exists (objMatch.Value) Then
strSearchString = Replace(strSearchString, objMatch.Value, "#Phone" & vbCRLF & Chr(34) & "4047895400" & Chr(34))
icount = icount + 1
'WRITE TO REPLACED#s.LOG WITH PHONE NUMBERS THAT HAVE BEEN REPLACED
Set objFile = objFSO.OpenTextFile("C:\RE
PLACED#s.L
OG", ForAppending)
objFile.WriteLine(objMatch
.Value) & " " & "replaced with phone number" & " " & Chr(34) & "4047895400" & Chr(34) & " " & dateStamp & " " & timestamp
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\RE
PLACED#s.L
OG")
strLogFileContents = objFile.ReadAll
objFile.Close
End If
Next
'WRITE TO datafile.txt REPLACING INVALID PHONE NUMBERS
Set objFile = objFSO.OpenTextFile("c:\da
tafile.txt
", ForWriting)
objFile.WriteLine strSearchString
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\RE
PLACED#s.L
OG", ForWriting)
objFile.WriteLine "Found " & iCount & " matches total."
objFile.WriteLine strLogFileContents
objFile.Close
Start Free Trial