By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,276 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,276 IT Pros & Developers. It's quick & easy.

Find and Replace question

P: n/a
I have a text file that contains about 8 to 10 text sequences that I need to
replace.

I want to search and replace all 8 to 10 text sequence anytime I run this
script

Here is what I have so far.
Const ForReading = 1
Const ForWriting = 2

strHostFile = "C:\summat.dii"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strHostFile, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "@EDOC", "@C DocLink ")
strNewText = Replace(strText, "@C BegDoc", "@C BegDoc#")
'etc............

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

objFile.Close
The only text that is replaced it the last strNewText
i.e strNewText = Replace(strText, "@C BegDoc", "@C BegDoc#")

I am a novice at scripting so please be kind. :)

Could I use arrOldTexts = array("@EDOC", "@C BegDoc", etc......)

For Each strtexts in arrOldTexts


Jun 27 '08 #1
Share this Question
Share on Google+
15 Replies


P: n/a
"Mike "YO_BEE" B" <Mi********@discussions.microsoft.comschrieb
Set objFile = objFSO.OpenTextFile(strHostFile, ForWriting)
objFile.WriteLine strNewText
Looks like good old VB6 (or VBA) code. Appropriate group(s): m.p.vb.*
This one is about VB.Net (2002-2008)
Armin
Jun 27 '08 #2

P: n/a
Armin is right, this is VB6 code, not VB.Net. Comments inline anyway:

Mike "YO_BEE" B wrote:
strNewText = Replace(strText, "@EDOC", "@C DocLink ")
At this point strNewText contains the substitution; strText is
unchanged.
strNewText = Replace(strText, "@C BegDoc", "@C BegDoc#")
Therefore that line should be
strNewText = Replace(strNewText , "@C BegDoc", "@C BegDoc#")
>
Could I use arrOldTexts = array("@EDOC", "@C BegDoc", etc......)
You could use an array, but it won't support For Each; you'll need an
index. First, though, get it working as individual lines as above; then you
can look at using an array.
Jun 27 '08 #3

P: n/a
Can you give some more assistance?

So when I do the strText = objFile.ReadAll
Is this indexed at this point?
Where do I go from here?

This script will be a *.VBS

It will run the same procedure every time.
"Steve Gerrard" wrote:
Armin is right, this is VB6 code, not VB.Net. Comments inline anyway:

Mike "YO_BEE" B wrote:
strNewText = Replace(strText, "@EDOC", "@C DocLink ")

At this point strNewText contains the substitution; strText is
unchanged.
strNewText = Replace(strText, "@C BegDoc", "@C BegDoc#")

Therefore that line should be
strNewText = Replace(strNewText , "@C BegDoc", "@C BegDoc#")

Could I use arrOldTexts = array("@EDOC", "@C BegDoc", etc......)

You could use an array, but it won't support For Each; you'll need an
index. First, though, get it working as individual lines as above; then you
can look at using an array.
Jun 27 '08 #4

P: n/a
Hi Mike.
When you have the expression "strNewText = Replace(strText, "@C BegDoc", "@C
BegDoc#")" it means you replaced all the ""@C BegDoc" wih the ""@C BegDoc#"
from the variable strText and you take the result in the variable
strNewText. So you have to repeat more similar actions in to this new
variable (strNewText) each time, passing it as an argument in the Replace
function. Your mistake is that you pass each time the original (unchanged)
variable (strText).

"Mike "YO_BEE" B" <Mi********@discussions.microsoft.comwrote in message
news:A2**********************************@microsof t.com...
Can you give some more assistance?

So when I do the strText = objFile.ReadAll
Is this indexed at this point?
Where do I go from here?

This script will be a *.VBS

It will run the same procedure every time.
"Steve Gerrard" wrote:
>Armin is right, this is VB6 code, not VB.Net. Comments inline anyway:

Mike "YO_BEE" B wrote:
strNewText = Replace(strText, "@EDOC", "@C DocLink ")

At this point strNewText contains the substitution; strText is
unchanged.
strNewText = Replace(strText, "@C BegDoc", "@C BegDoc#")

Therefore that line should be
strNewText = Replace(strNewText , "@C BegDoc", "@C BegDoc#")
>
Could I use arrOldTexts = array("@EDOC", "@C BegDoc", etc......)

You could use an array, but it won't support For Each; you'll need an
index. First, though, get it working as individual lines as above; then
you
can look at using an array.
Jun 27 '08 #5

P: n/a
So some sort of loop.

If that is the case how would you apply a loop to my expression?

"Paul" wrote:
Hi Mike.
When you have the expression "strNewText = Replace(strText, "@C BegDoc", "@C
BegDoc#")" it means you replaced all the ""@C BegDoc" wih the ""@C BegDoc#"
from the variable strText and you take the result in the variable
strNewText. So you have to repeat more similar actions in to this new
variable (strNewText) each time, passing it as an argument in the Replace
function. Your mistake is that you pass each time the original (unchanged)
variable (strText).

"Mike "YO_BEE" B" <Mi********@discussions.microsoft.comwrote in message
news:A2**********************************@microsof t.com...
Can you give some more assistance?

So when I do the strText = objFile.ReadAll
Is this indexed at this point?
Where do I go from here?

This script will be a *.VBS

It will run the same procedure every time.
"Steve Gerrard" wrote:
Armin is right, this is VB6 code, not VB.Net. Comments inline anyway:

Mike "YO_BEE" B wrote:
strNewText = Replace(strText, "@EDOC", "@C DocLink ")

At this point strNewText contains the substitution; strText is
unchanged.

strNewText = Replace(strText, "@C BegDoc", "@C BegDoc#")

Therefore that line should be
strNewText = Replace(strNewText , "@C BegDoc", "@C BegDoc#")


Could I use arrOldTexts = array("@EDOC", "@C BegDoc", etc......)


You could use an array, but it won't support For Each; you'll need an
index. First, though, get it working as individual lines as above; then
you
can look at using an array.
Jun 27 '08 #6

P: n/a
Mike "YO_BEE" B wrote:
So some sort of loop.

If that is the case how would you apply a loop to my expression?
Dim strText As String = "@C BegDoc12 Hello, @EDOCWorld"

Dim strSubs As String(,) = { _
{"@EDOC", "@C DocLink "}, _
{"@C BegDoc", "@C BegDoc#"} }

Dim strNewText As String = strText
For n As Integer = 0 To strSubs.GetUpperBound(0)
strNewText = Replace(strNewText, strSubs(n, 0), strSubs(n, 1))
Next n

Debug.Print(strNewText)
Jun 27 '08 #7

P: n/a
I am having a difficult time understanding this stuff. Like I said in the
first post of this thread, " I am a newbie at this stuff" Sorry for my lack
of knowledge.

Steve,

Your post is it a test script with nothing else or should I add it to my
original script?

"Steve Gerrard" wrote:
Mike "YO_BEE" B wrote:
So some sort of loop.

If that is the case how would you apply a loop to my expression?

Dim strText As String = "@C BegDoc12 Hello, @EDOCWorld"

Dim strSubs As String(,) = { _
{"@EDOC", "@C DocLink "}, _
{"@C BegDoc", "@C BegDoc#"} }

Dim strNewText As String = strText
For n As Integer = 0 To strSubs.GetUpperBound(0)
strNewText = Replace(strNewText, strSubs(n, 0), strSubs(n, 1))
Next n

Debug.Print(strNewText)
Jun 27 '08 #8

P: n/a
Mike "YO_BEE" B wrote:
I am having a difficult time understanding this stuff. Like I said
in the first post of this thread, " I am a newbie at this stuff"
Sorry for my lack of knowledge.

Steve,

Your post is it a test script with nothing else or should I add it to
my original script?
Gary, if you are having trouble, stick to the simple stuff first.

Modify your original code with the suggestion that Paul and I made. Don't pass
Go, don't collect $200, until you have that working, and understand how it
works. To repeat it: you need to do the second substitution with the result of
the first one, not the original string. That means using strNewText instead of
strText.

Get that working, post the working result back here, and only then ask questions
about loops and arrays.
Jun 27 '08 #9

P: n/a
Here is my modified code as per Paul and Steve

Const ForReading = 1
Const ForWriting = 2

strHostFile = "C:\summat.dii"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strHostFile, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "@EDOC", "@C DocLink ")
strNewText1 = Replace(strNewText, "@C BegDoc", "@C BegDoc#")'
'wscript.echo strNewText1

Set objFile = objFSO.OpenTextFile(strHostFile, ForWriting)

objFile.WriteLine strNewText1
objFile.Close
"Steve Gerrard" wrote:
Mike "YO_BEE" B wrote:
I am having a difficult time understanding this stuff. Like I said
in the first post of this thread, " I am a newbie at this stuff"
Sorry for my lack of knowledge.

Steve,

Your post is it a test script with nothing else or should I add it to
my original script?

Gary, if you are having trouble, stick to the simple stuff first.

Modify your original code with the suggestion that Paul and I made. Don't pass
Go, don't collect $200, until you have that working, and understand how it
works. To repeat it: you need to do the second substitution with the result of
the first one, not the original string. That means using strNewText instead of
strText.

Get that working, post the working result back here, and only then ask questions
about loops and arrays.
Jun 27 '08 #10

P: n/a
Mike "YO_BEE" B wrote:
Here is my modified code as per Paul and Steve

strNewText = Replace(strText, "@EDOC", "@C DocLink ")
strNewText1 = Replace(strNewText, "@C BegDoc", "@C BegDoc#")'
'wscript.echo strNewText1
Okay, lets change that a little bit, with an eye toward doing a loop.

You don't need to create a third variable, strNewText1. It is okay to assign the
result of a Replace back to the same variable. While we are at it, I will add
another line at the beginning, making strNewText the same as strText to start
out with. This will help setup the pattern of your statements, so you can begin
to see how to make a loop.

strNewText = strText

' now we just work with strNewText

strNewText = Replace(strNewText, "@EDOC", "@C DocLink ")
strNewText = Replace(strNewText, "@C BegDoc", "@C BegDoc#")

' notice how similar those two lines are, and which parts are different.

wscript.echo strNewText
Jun 27 '08 #11

P: n/a
Thank you Steve for te help it worked
"Steve Gerrard" wrote:
Mike "YO_BEE" B wrote:
Here is my modified code as per Paul and Steve

strNewText = Replace(strText, "@EDOC", "@C DocLink ")
strNewText1 = Replace(strNewText, "@C BegDoc", "@C BegDoc#")'
'wscript.echo strNewText1

Okay, lets change that a little bit, with an eye toward doing a loop.

You don't need to create a third variable, strNewText1. It is okay to assign the
result of a Replace back to the same variable. While we are at it, I will add
another line at the beginning, making strNewText the same as strText to start
out with. This will help setup the pattern of your statements, so you can begin
to see how to make a loop.

strNewText = strText

' now we just work with strNewText

strNewText = Replace(strNewText, "@EDOC", "@C DocLink ")
strNewText = Replace(strNewText, "@C BegDoc", "@C BegDoc#")

' notice how similar those two lines are, and which parts are different.

wscript.echo strNewText
Jun 27 '08 #12

P: n/a
Mike "YO_BEE" B wrote:
Thank you Steve for te help it worked
>strNewText = Replace(strNewText, "@EDOC", "@C DocLink ")
strNewText = Replace(strNewText, "@C BegDoc", "@C BegDoc#")

' notice how similar those two lines are, and which parts are
different.
Okay. Do you see that the last two strings are the part that varies on each
line, and the rest is the same? What we need are arrays that will let us use
variables for those last two strings, so we can make it a loop, instead of
repeating the line.

Here is the basic array syntax:
Dim strOld(1) As String
Dim strNew(1) As String

strOld(0) = ""@EDOC"
strNew(0) = ""@C DocLink "

strOld(1) = ""@C BegDoc"
strNew(1) = ""@C BegDoc#"

Here is a simple loop to check our arrays:
Dim n As Integer
For n = 0 To 1
Debug.Print strOld(n) + "; " + strNew(n)
' or wscript.echo strOld(n) + "; " + strNew(n)
Next n

See if that works, and you understand what the arrays are, and how the loop
works. Then we can put it all together.


Jun 27 '08 #13

P: n/a
So for me to add the next step will I have to create 14 strOld and 14 strNew,
because the code I pasted solved my Dilemma

here is my code.
Const ForReading = 1
Const ForWriting = 2

strHostFile = "C:\summat.dii"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strHostFile, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = strText

' notice how similar those two lines are, and which parts are different.

'here is your first step that I applied then
'added all 14 static words
'strNewText = Replace(strText, "@EDOC", "@C DocLink ")
'strNewText = Replace(strNewText, "@C BegDoc", "@C BegDoc#")
'wscript.echo strNewText
' now we just work with strNewText

strNewText = Replace(strNewText, "@EDOC ", "@C Doclink ")
strNewText = Replace(strNewText, "@C BegDoc ", "@C BegDoc# ")
strNewText = Replace(strNewText, "@C ENDDoc ", "@C ENDDoc# ")
strNewText = Replace(strNewText, "@C BEGATT ", "@C BEGATT# ")
strNewText = Replace(strNewText, "@C ENDATT ", "@C ENDATT# ")
strNewText = Replace(strNewText, "@C PAGECNT ", "@C PGcount ")
strNewText = Replace(strNewText, "@C Source ", "@C Sources ")
strNewText = Replace(strNewText, "@FROM ", "@C Author ")
strNewText = Replace(strNewText, "@C Recipient ", "@C To ")
strNewText = Replace(strNewText, "@C Name ", "@C Names ")
strNewText = Replace(strNewText, "@C Copyee ", "@C CC ")
strNewText = Replace(strNewText, "@DATECREATED ", "@C Datecrtd ")
strNewText = Replace(strNewText, "@APPLICATION ", "@C Applicat ")
strNewText = Replace(strNewText, "@DATESAVED ", "@C Datesvd ")

Set objFile = objFSO.OpenTextFile(strHostFile, ForWriting)
objFile.WriteLine strNewText
objFile.Close
"Steve Gerrard" wrote:

Mike "YO_BEE" B wrote:
Thank you Steve for te help it worked
strNewText = Replace(strNewText, "@EDOC", "@C DocLink ")
strNewText = Replace(strNewText, "@C BegDoc", "@C BegDoc#")

' notice how similar those two lines are, and which parts are
different.

Okay. Do you see that the last two strings are the part that varies on each
line, and the rest is the same? What we need are arrays that will let us use
variables for those last two strings, so we can make it a loop, instead of
repeating the line.

Here is the basic array syntax:
Dim strOld(1) As String
Dim strNew(1) As String

strOld(0) = ""@EDOC"
strNew(0) = ""@C DocLink "

strOld(1) = ""@C BegDoc"
strNew(1) = ""@C BegDoc#"

Here is a simple loop to check our arrays:
Dim n As Integer
For n = 0 To 1
Debug.Print strOld(n) + "; " + strNew(n)
' or wscript.echo strOld(n) + "; " + strNew(n)
Next n

See if that works, and you understand what the arrays are, and how the loop
works. Then we can put it all together.


Jun 27 '08 #14

P: n/a
Mike "YO_BEE" B wrote:
So for me to add the next step will I have to create 14 strOld and 14
strNew, because the code I pasted solved my Dilemma
You will have to define the 14 old and 14 new strings somewhere, yes. No mind
reading allowed. :)

If you define them in arrays, then the code itself can be a loop:
For n = 0 To 13
strNewText = Replace(strNewText, strOld(n), strNew(n))
Next n
Is it better to have 14 lines defining the old and new elements, and then a
short loop, or just have 14 Replace lines? I don't know, that is your call. It
depends on what else you might be doing, I guess. If the 14 Replace statements
works for you, and you don't need the arrays for something else, then you might
as well just leave it at that.

Jun 27 '08 #15

P: n/a
I am going to leave it now. I will play around with the Strings also and see
what I can do.

"Steve Gerrard" wrote:
Mike "YO_BEE" B wrote:
So for me to add the next step will I have to create 14 strOld and 14
strNew, because the code I pasted solved my Dilemma

You will have to define the 14 old and 14 new strings somewhere, yes. No mind
reading allowed. :)

If you define them in arrays, then the code itself can be a loop:
For n = 0 To 13
strNewText = Replace(strNewText, strOld(n), strNew(n))
Next n
Is it better to have 14 lines defining the old and new elements, and then a
short loop, or just have 14 Replace lines? I don't know, that is your call. It
depends on what else you might be doing, I guess. If the 14 Replace statements
works for you, and you don't need the arrays for something else, then you might
as well just leave it at that.

Jun 27 '08 #16

This discussion thread is closed

Replies have been disabled for this discussion.