VB script to replace a string of text that changes in a text file

I've been trying like hell to replace a variable length string of text contained withing Par's, like below

203.202.188.218/2559 (203.202.188.218/2559) to dmz

I need to somehow delete everything within the parenthases (spell) and the parenthasese themselves, I've been using find replace like below

  set fil = fso.OpenTextFile(i, 1)
         s = fil.ReadAll
         fil.Close
     set fil = nothing
s1 = "("
s2 = ", "
  proc = replace(s, s1, s2, 1, -1, 1)  
fso.DeleteFile i, true
set fil = fso.CreateTextFile(i)
fil.Write proc
fil.Close
set fil = nothing


BUT, I can't seem to get s1 = to take a variable like replace "( <and everything here in between and stop at> )"
everytime I try like s1 = "(%)" it just looks for that actual text (%) not a variable, any clues?  I'm stuck!!!
Viper6Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

keenezCommented:
I would first split the string

Dim x
x = split(s, "(")

x(0) would equal 203.202.188.218/2559
x(1) would equal 203.202.188.218/2559) to dmz

You could then do s = x(0) & right(x(1), len(x(1)) - instr(x(1), ")"))

or split x(1) again like

Dim y
y = split(x(1), ")")

s = x(0) & y(1)

Cheers,

Keenez
0
Richie_SimonettiIT OperationsCommented:
replace with
proc = Mid$(s, 1, InStr(1, s, "(", vbTextCompare) - 1) & Mid$(s, InStr(1, s, ")", vbTextCompare) + 1)
0
Viper6Author Commented:
I don't think splitting will work, the IP changes in every line, or I'm not reading this right.

Richie, I'm new at VB and I don't quite understand yoru line there.

what do I need to replace with that code, my proc replace line?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Richie_SimonettiIT OperationsCommented:
I think this would be the complete code:

set fil = fso.OpenTextFile(i, 1)
        s = fil.ReadAll
        fil.Close
    set fil = nothing
proc = Mid$(s, 1, InStr(1, s, "(", vbTextCompare) - 1) & Mid$(s, InStr(1, s, ")", vbTextCompare) + 1)
 
fso.DeleteFile i, true
set fil = fso.CreateTextFile(i)
fil.Write proc
fil.Close
set fil = nothing
0
Richie_SimonettiIT OperationsCommented:
If you need to change the data that it is inside (), just add it:
example:
proc = Mid$(s, 1, InStr(1, s, "(", vbTextCompare) - 1) &
 "New_line_entered_here" & Mid$(s, InStr(1, s, ")", vbTextCompare) + 1)
0
Richie_SimonettiIT OperationsCommented:
Or this is what you need?

proc = Mid$(s, 1, InStr(1, s, "(", vbTextCompare)) &
"dmz" & Mid$(s, InStr(1, s, ")", vbTextCompare))
0
keenezCommented:
Actually, even though the IP is changing, my algorithm would still work.

First, I think of "(" as a delimiter.  The split command would parse the string into an array.  You can think of it like a csv file when you open it up in Excel.  Excel looks as "," as a seperator.  In this situation, I'm looking at the "(" as a seperator.

The first part of the string would be placed in element 0 of the array.  Split then removes the "(" and places everything after that into element 1.  If split hits another "(", it will remove that and start placing everything after that in element 2 and so on.....  Essentially, we're using instr to find the "(" but split is doing all the work of seperating into the parts.

Once your string has been parsed and the "(" removed .... all that's left is to find in each string the ")" and take everything after it.  You can use the mid or right command or even use split again except for ")" this time.  Split will tell you everything before the ")" and everything after it so this would be independent of words

Eg.

Dim str as string, x as variant
str = "blah blah blah (Something here) more stuff"

x = split(str, "(")
' x(0) would be - blah blah blah
' x(1) would be - Something here) more stuff

if you wanted to get everything after the ), you could do

Dim y as variant
y = split(x(1), ")")
' y(0) would be - Something here
' y(1) would be -  more stuff

To put together the string removing the parentheseses and everything between them:

str = x(0) & y(1)

In essence, the "algorithm" that Richie and I are proposing are the same.  One advantage of my proposed method is that multiple (....) can be looked for.  One disadvantage is if you have ( .... ( ....)) but this case also requires a little more complexity to Richie's code as well.  

Cheers,

Keenez
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CleanupPingCommented:
Viper6:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.