Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

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!!!
0
Viper6
Asked:
Viper6
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now