Computing.Net > Forums > Programming > vbs search & replace w/a twist

vbs search & replace w/a twist

Reply to Message Icon

Original Message
Name: edman747
Date: August 30, 2007 at 08:40:14 Pacific
Subject: vbs search & replace w/a twist
OS: xp home
CPU/Ram: 2.8ghz p4/1gb
Model/Manufacturer: hp/pavilion zv5160us
Comment:

Hello All,
I would like to tweak the following vb script to search for multiple text strings. And add two lines after each match. Then append multiple lines to the end of the file.

am a little confused about how to organize this. any help would be appreciated.

simple search & replace script.
[code]
Dim f, fc, afile
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set f = objfso.GetFolder(".")
Set fc = f.Files

For Each aFile In fc
'If UCase(Right(aFile.Name, 3)) = "ENT" then
If Left(afile.Type, 3) = "ENT" Then

Set objFile = objFSO.OpenTextFile(afile, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "mp_multiplespawn 0", "mp_multiplespawn 1")

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

End If
Next
[/code]

the text I wish to search for:
alien_slave
barnacle
headcrab
houndeye
zombie
would like to add two lines after each match.
and increment a match_counter.

example:
"monstertype" "monster_headcrab"
becomes:
"monstertype" "monster_headcrab"
"TriggerTarget" "MonsterCounter"
"TriggerCondition" "4"

Then append the following text to the end of the file.
{
"target" "EndMap"
"count" "value of match_counter"
"style" "32"
"message" "one dead monster"
"targetname" "MonsterCounter"
"classname" "trigger_counter"
}
{
"targetname" "EndMap"
"model" "*4"
"landmark" "c1a1"
"map" "c1a1a"
"spawnflags" "2"
"classname" "trigger_changelevel"
}


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: August 31, 2007 at 05:19:34 Pacific
Reply: (edit)

Given your already working code base, all you have to do is modify your strNewText = Replace(... line. Specifically, you'll need to use the constant vbNewLine.

Specifically, you'll end up with something like this:
strNewText = Replace(strText, """monstertype"" ""monster_headcrab""", """monstertype"" ""monster_headcrab""" & vbNewLine & """TriggerTarget"" ""MonsterCounter""" & vbNewLine & """TriggerCondition"" ""4""")

It's hardly best coding practices, nor is it pretty, but it'll do the job, and that's what counts, right?


Report Offensive Follow Up For Removal

Response Number 2
Name: edman747
Date: August 31, 2007 at 08:18:51 Pacific
Reply: (edit)

thank you for the great reply. that gets half the job done. and I never would have thought of it.

now all I have to work on is incrementing a counter with each match. then appending several lines at the EOF with the count embedded.


Report Offensive Follow Up For Removal

Response Number 3
Name: Razor2.3
Date: August 31, 2007 at 09:12:02 Pacific
Reply: (edit)

In keeping with the theme of bending the code to our will, in spite of all reason, you could do something like this for your count:

With New RegExp
.IgnoreCase = True
.Global = True
.Pattern = """monstertype"" ""monster_headcrab"""
iCount = .Execute.Count
End With

...Or something to that effect.

Adding text to the end isn't an issue, surprisingly. After the .WriteLine and before the .Close, add as many objFile.WriteLine "Text to add" lines as needed.


Report Offensive Follow Up For Removal

Response Number 4
Name: edman747
Date: August 31, 2007 at 12:41:40 Pacific
Reply: (edit)

once again thanks for your reply. this is a little hard to add any additional text items to search for (monster_human_grunt). While keeping track of the current monster count or making any other changes. but it works.


Dim f, fc, afile
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set f = objfso.GetFolder(".")
Set fc = f.Files

For Each aFile In fc
'If UCase(Right(aFile.Name, 3)) = "ENT" then
If Left(afile.Type, 3) = "ENT" Then

Set objFile = objFSO.OpenTextFile(afile, ForReading)
strText = objFile.ReadAll
objFile.Close

Dim colmatches
'alien_slave counter
With New RegExp
.IgnoreCase = True
.Global = True
.Pattern = "monster_alien_slave"
Set colMatches = .Execute(strText)
ialien_slavescount = colmatches.count
End With

'barnacle counter
With New RegExp
.IgnoreCase = True
.Global = True
.Pattern = "monster_barnacle"
Set colMatches = .Execute(strText)
ibarnaclecount = colmatches.count
End With

'headcrab counter
With New RegExp
.IgnoreCase = True
.Global = True
.Pattern = "monster_headcrab"
Set colMatches = .Execute(strText)
iheadcrabcount = colmatches.count
End With

'houndeye counter
With New RegExp
.IgnoreCase = True
.Global = True
.Pattern = "monster_houndeye"
Set colMatches = .Execute(strText)
ihoundeyecount = colmatches.count
End With

'zombie counter
'Dim colmatches
With New RegExp
.IgnoreCase = True
.Global = True
.Pattern = "monster_zombie"
Set colMatches = .Execute(strText)
izombiecount = colmatches.count
End With

'sum the counters
icount = ialien_slavecount + ibarnaclecount + iheadcrabcount + ihoundeyecount + izombiecount

'insert headcrab stuff
strstuff = Chr(34) & "monster_headcrab" &Chr(34)
strNewText = Replace(strText, _
strstuff, _
strstuff _
&vbNewLine &Chr(34) &"TriggerTarget" &Chr(34) & " " &chr(34) &"MonsterCounter" &Chr(34) _
&vbNewLine &Chr(34) & "TriggerCondition" &Chr(34) & " " &chr(34) & "4" &Chr(34))
If iheadcrabcount > 0 Then
strText = strNewText
End If

'insert houndeye stuff
strstuff = Chr(34) & "monster_houndeye" &Chr(34)
strNewText = Replace(strText, _
strstuff, _
strstuff _
&vbNewLine &Chr(34) &"TriggerTarget" &Chr(34) & " " &chr(34) &"MonsterCounter" &Chr(34) _
&vbNewLine &Chr(34) & "TriggerCondition" &Chr(34) & " " &chr(34) & "4" &Chr(34))
If ihoundeyecount > 0 Then
strText = strNewText
End If

'insert zombie stuff
strstuff = Chr(34) & "monster_zombie" &Chr(34)
strNewText = Replace(strText, _
strstuff, _
strstuff _
&vbNewLine &Chr(34) &"TriggerTarget" &Chr(34) & " " &chr(34) &"MonsterCounter" &Chr(34) _
&vbNewLine &Chr(34) & "TriggerCondition" &Chr(34) & " " &chr(34) & "4" &Chr(34))
If izombiecount > 0 Then
strText = strNewText
End If

Set objFile = objFSO.OpenTextFile(afile, ForWriting)
objFile.WriteLine strNewText
'append other stuff
objFile.WriteLine "{"
objFile.WriteLine chr(34) &"target" &Chr(34) &" " &chr(34) &"EndMap" &Chr(34)
objFile.WriteLine Chr(34) &"count"&Chr(34) &" " &Chr(34) &iCount-2 &Chr(34)
objFile.WriteLine Chr(34) &"style"&Chr(34) &" " &Chr(34) &"32" &Chr(34)
objFile.WriteLine Chr(34) &"message" &Chr(34) &" " &Chr(34) &"one dead monster" &Chr(34)
objFile.WriteLine Chr(34) &"targetname" &Chr(34) &" " &Chr(34) &"MonsterCounter" &Chr(34)
objFile.WriteLine Chr(34) &"classname" &Chr(34) &" " &Chr(34) &"trigger_counter" &Chr(34)
objFile.WriteLine "}"
objFile.WriteLine "{"
objFile.WriteLine Chr(34) &"targetname" &Chr(34) &" " &Chr(34) &"EndMap" &Chr(34)
objFile.WriteLine Chr(34) &"spawnflags" &Chr(34) &" " &Chr(34) &"2" &Chr(34)
objFile.WriteLine Chr(34) &"classname" &Chr(34) &" " &Chr(34) &"trigger_changelevel" &Chr(34)
objFile.WriteLine Chr(34) &"map" &Chr(34) & " " &Chr(34) &"WHATEVER" &Chr(34)
objFile.WriteLine "}"

objFile.Close

End If
Next



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: vbs search & replace w/a twist

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge