?
Solved

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

Posted on 2003-03-11
8
Medium Priority
?
155 Views
Last Modified: 2010-04-07
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
Comment
Question by:Viper6
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Expert Comment

by:keenez
ID: 8114455
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8114479
replace with
proc = Mid$(s, 1, InStr(1, s, "(", vbTextCompare) - 1) & Mid$(s, InStr(1, s, ")", vbTextCompare) + 1)
0
 

Author Comment

by:Viper6
ID: 8114725
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8119829
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8119839
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8119852
Or this is what you need?

proc = Mid$(s, 1, InStr(1, s, "(", vbTextCompare)) &
"dmz" & Mid$(s, InStr(1, s, ")", vbTextCompare))
0
 
LVL 2

Accepted Solution

by:
keenez earned 225 total points
ID: 8120148
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
 

Expert Comment

by:CleanupPing
ID: 8900269
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question