473,396 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Find and Replace question

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
15 1871
"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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your...
26
by: rkleiner | last post by:
Is there a regular expression to find the first unmatched right bracket in a string, if there is one? For example, "(1*(2+3))))".search(/regexp/) == 9 Thanks in advance.
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
4
by: JackRazz | last post by:
I'm trying to use Visual Studio's Find/Replace to match VB declarations. This RegEx works fine in Regulator: ...
1
by: Daniel Miller | last post by:
Question 1: Is there a pre-made solution for implimenting Find & Replace for a textbox, or will I have to roll my own using String.IndexOf? Question 2: When I right-click on a file -> Open With...
2
by: :\\\\derian | last post by:
anyone know where i could find the equivalent of the find / replace windows control for my project? :\\derian
6
by: alainfri | last post by:
I am not sure if this group is the right place for this question but what I need is as follows. There is a piece of html. Throughout the html there are a lot of <brtags. The task is to replace all...
9
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient...
3
by: Crash | last post by:
VS2005 In the find/replace dialog if I make a regex like this I can find all of the "New" statements that instantiate a class with "System" in the name: <New>.*<System> But how - in the VS...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.