Find and Replace question

M

Mike YO_BEE B

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
 
A

Armin Zingler

Mike "YO_BEE" B said:
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
 
P

Paul

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).
 
M

Mike YO_BEE B

So some sort of loop.

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

Mike YO_BEE B

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

Mike YO_BEE B

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
 
M

Mike YO_BEE B

Thank you Steve for te help it worked


Steve Gerrard said:
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
 
M

Mike YO_BEE B

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


:
 
M

Mike YO_BEE B

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top