Link to home
Start Free TrialLog in
Avatar of Viper6
Viper6

asked on

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!!!
Avatar of keenez
keenez

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
Avatar of Richie_Simonetti
replace with
proc = Mid$(s, 1, InStr(1, s, "(", vbTextCompare) - 1) & Mid$(s, InStr(1, s, ")", vbTextCompare) + 1)
Avatar of Viper6

ASKER

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?
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
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)
Or this is what you need?

proc = Mid$(s, 1, InStr(1, s, "(", vbTextCompare)) &
"dmz" & Mid$(s, InStr(1, s, ")", vbTextCompare))
ASKER CERTIFIED SOLUTION
Avatar of keenez
keenez

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?