Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - script for multi text replacement

Reply
 
Old 06-15-2009   #1 (permalink)
tonycd


 
 

script for multi text replacement

Hi

I just copy the script from Microsoft web site that for replace the text.
But how can i enhance the script for multi - text replacement. Thanks
i try with this (i put the star in front of script) , but it doesn't work.

regards
Tony

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim ", "James ")
****strNewText = Replace(strText, "abc ", "apple ")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close



My System SpecsSystem Spec
Old 06-15-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: script for multi text replacement


"tonycd" <tonycd@xxxxxx> wrote in message
news:74DABA76-6B73-4153-9926-2726ABF13D41@xxxxxx
Quote:

> Hi
>
> I just copy the script from Microsoft web site that for replace the text.
> But how can i enhance the script for multi - text replacement. Thanks
> i try with this (i put the star in front of script) , but it doesn't work.
>
> regards
> Tony
>
> Const ForReading = 1
> Const ForWriting = 2
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
>
> strText = objFile.ReadAll
> objFile.Close
> strNewText = Replace(strText, "Jim ", "James ")
> ****strNewText = Replace(strText, "abc ", "apple ")
>
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
> objFile.WriteLine strNewText
> objFile.Close
>
>
You could nest your "Replace" function:
strNewText = Replace(Replace(strText, "Jim ", "James "), "abc", "apple")
or even better:
strNewText = Replace(Replace(strText, "Jim ", "James ",1,-1,1),"abc",
"apple",1,-1,1)


My System SpecsSystem Spec
Old 06-15-2009   #3 (permalink)
T Lavedas


 
 

Re: script for multi text replacement

On Jun 15, 6:53*am, tonycd <ton...@xxxxxx> wrote:
Quote:

> Hi
>
> I just copy the script from Microsoft web site that for replace the text.
> But how can i enhance the script for multi - text replacement. Thanks
> i try with this (i put the star in front of script) , but it doesn't work..
>
> regards
> Tony
>
> Const ForReading = 1
> Const ForWriting = 2
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
>
> strText = objFile.ReadAll
> objFile.Close
> strNewText = Replace(strText, "Jim ", "James ")
> ****strNewText = Replace(strText, "abc ", "apple ")
>
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
> objFile.WriteLine strNewText
> objFile.Close
The string you want to work with in the second replace is no longer
the version in the variable strText. It was changed and stored in
strNewText by the first replacement, so in the second line the correct
variable to work against is strNewText, as in ...

strNewText = Replace(strText, "Jim ", "James ")
****strNewText = Replace(strNEWText, "abc ", "apple ")

Tom Lavedas
************
My System SpecsSystem Spec
Old 06-15-2009   #4 (permalink)
Paul Randall


 
 

Re: script for multi text replacement


"tonycd" <tonycd@xxxxxx> wrote in message
news:74DABA76-6B73-4153-9926-2726ABF13D41@xxxxxx
Quote:

> Hi
>
> I just copy the script from Microsoft web site that for replace the text.
> But how can i enhance the script for multi - text replacement. Thanks
> i try with this (i put the star in front of script) , but it doesn't work.
>
> regards
> Tony
>
> Const ForReading = 1
> Const ForWriting = 2
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
>
> strText = objFile.ReadAll
> objFile.Close
> strNewText = Replace(strText, "Jim ", "James ")
> ****strNewText = Replace(strText, "abc ", "apple ")
>
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
> objFile.WriteLine strNewText
> objFile.Close
You probably already have file script56.chm on your computer. If not, go to
msdn.microsoft.com's download tab and search for and download it, and read
up on the replace function.

It says that there are potentially more parameters for the replace function:
Replace Function
See Also
Filter Function
Requirements
Version 2
Returns a string in which a specified substring has been replaced with
another substring a specified number of times.

Replace(expression, find, replacewith[, start[, count[, compare]]])
Arguments
expression
Required. String expression containing substring to replace.
find
Required. Substring being searched for.
replacewith
Required. Replacement substring.
start
Optional. Position within expression where substring search is to begin. If
omitted, 1 is assumed. Must be used in conjunction with count.
count
Optional. Number of substring substitutions to perform. If omitted, the
default value is -1, which means make all possible substitutions. Must be
used in conjunction with start.
compare
Optional. Numeric value indicating the kind of comparison to use when
evaluating substrings. See Settings section for values. If omitted, the
default value is 0, which means perform a binary comparison.

-Paul Randall


My System SpecsSystem Spec
Old 06-15-2009   #5 (permalink)
tonycd


 
 

Re: script for multi text replacement

Thanks all

What if i need to replace the text more than ten. Thanks again
regards
Tony

"Paul Randall" wrote:
Quote:

>
> "tonycd" <tonycd@xxxxxx> wrote in message
> news:74DABA76-6B73-4153-9926-2726ABF13D41@xxxxxx
Quote:

> > Hi
> >
> > I just copy the script from Microsoft web site that for replace the text.
> > But how can i enhance the script for multi - text replacement. Thanks
> > i try with this (i put the star in front of script) , but it doesn't work.
> >
> > regards
> > Tony
> >
> > Const ForReading = 1
> > Const ForWriting = 2
> >
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
> >
> > strText = objFile.ReadAll
> > objFile.Close
> > strNewText = Replace(strText, "Jim ", "James ")
> > ****strNewText = Replace(strText, "abc ", "apple ")
> >
> > Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
> > objFile.WriteLine strNewText
> > objFile.Close
>
> You probably already have file script56.chm on your computer. If not, go to
> msdn.microsoft.com's download tab and search for and download it, and read
> up on the replace function.
>
> It says that there are potentially more parameters for the replace function:
> Replace Function
> See Also
> Filter Function
> Requirements
> Version 2
> Returns a string in which a specified substring has been replaced with
> another substring a specified number of times.
>
> Replace(expression, find, replacewith[, start[, count[, compare]]])
> Arguments
> expression
> Required. String expression containing substring to replace.
> find
> Required. Substring being searched for.
> replacewith
> Required. Replacement substring.
> start
> Optional. Position within expression where substring search is to begin. If
> omitted, 1 is assumed. Must be used in conjunction with count.
> count
> Optional. Number of substring substitutions to perform. If omitted, the
> default value is -1, which means make all possible substitutions. Must be
> used in conjunction with start.
> compare
> Optional. Numeric value indicating the kind of comparison to use when
> evaluating substrings. See Settings section for values. If omitted, the
> default value is 0, which means perform a binary comparison.
>
> -Paul Randall
>
>
>
My System SpecsSystem Spec
Old 06-15-2009   #6 (permalink)
Pegasus [MVP]


 
 

Re: script for multi text replacement

Sorry, can't understand "What if i need to replace the text more than ten" -
please rephrase.


"tonycd" <tonycd@xxxxxx> wrote in message
news:69C4CC27-9A58-41F3-830F-3FC5972B4F55@xxxxxx
Quote:

> Thanks all
>
> What if i need to replace the text more than ten. Thanks again
> regards
> Tony
>
> "Paul Randall" wrote:
>
Quote:

>>
>> "tonycd" <tonycd@xxxxxx> wrote in message
>> news:74DABA76-6B73-4153-9926-2726ABF13D41@xxxxxx
Quote:

>> > Hi
>> >
>> > I just copy the script from Microsoft web site that for replace the
>> > text.
>> > But how can i enhance the script for multi - text replacement. Thanks
>> > i try with this (i put the star in front of script) , but it doesn't
>> > work.
>> >
>> > regards
>> > Tony
>> >
>> > Const ForReading = 1
>> > Const ForWriting = 2
>> >
>> > Set objFSO = CreateObject("Scripting.FileSystemObject")
>> > Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
>> >
>> > strText = objFile.ReadAll
>> > objFile.Close
>> > strNewText = Replace(strText, "Jim ", "James ")
>> > ****strNewText = Replace(strText, "abc ", "apple ")
>> >
>> > Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
>> > objFile.WriteLine strNewText
>> > objFile.Close
>>
>> You probably already have file script56.chm on your computer. If not, go
>> to
>> msdn.microsoft.com's download tab and search for and download it, and
>> read
>> up on the replace function.
>>
>> It says that there are potentially more parameters for the replace
>> function:
>> Replace Function
>> See Also
>> Filter Function
>> Requirements
>> Version 2
>> Returns a string in which a specified substring has been replaced with
>> another substring a specified number of times.
>>
>> Replace(expression, find, replacewith[, start[, count[, compare]]])
>> Arguments
>> expression
>> Required. String expression containing substring to replace.
>> find
>> Required. Substring being searched for.
>> replacewith
>> Required. Replacement substring.
>> start
>> Optional. Position within expression where substring search is to begin.
>> If
>> omitted, 1 is assumed. Must be used in conjunction with count.
>> count
>> Optional. Number of substring substitutions to perform. If omitted, the
>> default value is -1, which means make all possible substitutions. Must be
>> used in conjunction with start.
>> compare
>> Optional. Numeric value indicating the kind of comparison to use when
>> evaluating substrings. See Settings section for values. If omitted, the
>> default value is 0, which means perform a binary comparison.
>>
>> -Paul Randall
>>
>>
>>

My System SpecsSystem Spec
Old 06-15-2009   #7 (permalink)
tonycd


 
 

Re: script for multi text replacement

Hi Pegasus

thanks for your help again. As i mentioned before . i need to replace from
"event no." to "event description" from audit logon report. So more than ten
"event no." that i need to replace. Would you please help ..Thanks

regards
Tony

"Pegasus [MVP]" wrote:
Quote:

> Sorry, can't understand "What if i need to replace the text more than ten" -
> please rephrase.
>
>
> "tonycd" <tonycd@xxxxxx> wrote in message
> news:69C4CC27-9A58-41F3-830F-3FC5972B4F55@xxxxxx
Quote:

> > Thanks all
> >
> > What if i need to replace the text more than ten. Thanks again
> > regards
> > Tony
> >
> > "Paul Randall" wrote:
> >
Quote:

> >>
> >> "tonycd" <tonycd@xxxxxx> wrote in message
> >> news:74DABA76-6B73-4153-9926-2726ABF13D41@xxxxxx
> >> > Hi
> >> >
> >> > I just copy the script from Microsoft web site that for replace the
> >> > text.
> >> > But how can i enhance the script for multi - text replacement. Thanks
> >> > i try with this (i put the star in front of script) , but it doesn't
> >> > work.
> >> >
> >> > regards
> >> > Tony
> >> >
> >> > Const ForReading = 1
> >> > Const ForWriting = 2
> >> >
> >> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> >> > Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
> >> >
> >> > strText = objFile.ReadAll
> >> > objFile.Close
> >> > strNewText = Replace(strText, "Jim ", "James ")
> >> > ****strNewText = Replace(strText, "abc ", "apple ")
> >> >
> >> > Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
> >> > objFile.WriteLine strNewText
> >> > objFile.Close
> >>
> >> You probably already have file script56.chm on your computer. If not, go
> >> to
> >> msdn.microsoft.com's download tab and search for and download it, and
> >> read
> >> up on the replace function.
> >>
> >> It says that there are potentially more parameters for the replace
> >> function:
> >> Replace Function
> >> See Also
> >> Filter Function
> >> Requirements
> >> Version 2
> >> Returns a string in which a specified substring has been replaced with
> >> another substring a specified number of times.
> >>
> >> Replace(expression, find, replacewith[, start[, count[, compare]]])
> >> Arguments
> >> expression
> >> Required. String expression containing substring to replace.
> >> find
> >> Required. Substring being searched for.
> >> replacewith
> >> Required. Replacement substring.
> >> start
> >> Optional. Position within expression where substring search is to begin.
> >> If
> >> omitted, 1 is assumed. Must be used in conjunction with count.
> >> count
> >> Optional. Number of substring substitutions to perform. If omitted, the
> >> default value is -1, which means make all possible substitutions. Must be
> >> used in conjunction with start.
> >> compare
> >> Optional. Numeric value indicating the kind of comparison to use when
> >> evaluating substrings. See Settings section for values. If omitted, the
> >> default value is 0, which means perform a binary comparison.
> >>
> >> -Paul Randall
> >>
> >>
> >>
>
>
>
My System SpecsSystem Spec
Old 06-15-2009   #8 (permalink)
Pegasus [MVP]


 
 

Re: script for multi text replacement

I quote from Paul Randall's reply (which he copied from script56.chm):

================
Optional. Number of substring substitutions to perform. If omitted, the
default value is -1, which means make all possible substitutions. Must be
used in conjunction with start.

================

In other words, *all* matching strings will be replaced. Why don't you give
it a try?


"tonycd" <tonycd@xxxxxx> wrote in message
news:44689407-F42D-4CA9-AC49-0B94B3DF877F@xxxxxx
Quote:

> Hi Pegasus
>
> thanks for your help again. As i mentioned before . i need to replace from
> "event no." to "event description" from audit logon report. So more than
> ten
> "event no." that i need to replace. Would you please help ..Thanks
>
> regards
> Tony
>
> "Pegasus [MVP]" wrote:
>
Quote:

>> Sorry, can't understand "What if i need to replace the text more than
>> ten" -
>> please rephrase.
>>
>>
>> "tonycd" <tonycd@xxxxxx> wrote in message
>> news:69C4CC27-9A58-41F3-830F-3FC5972B4F55@xxxxxx
Quote:

>> > Thanks all
>> >
>> > What if i need to replace the text more than ten. Thanks again
>> > regards
>> > Tony
>> >
>> > "Paul Randall" wrote:
>> >
>> >>
>> >> "tonycd" <tonycd@xxxxxx> wrote in message
>> >> news:74DABA76-6B73-4153-9926-2726ABF13D41@xxxxxx
>> >> > Hi
>> >> >
>> >> > I just copy the script from Microsoft web site that for replace the
>> >> > text.
>> >> > But how can i enhance the script for multi - text replacement.
>> >> > Thanks
>> >> > i try with this (i put the star in front of script) , but it doesn't
>> >> > work.
>> >> >
>> >> > regards
>> >> > Tony
>> >> >
>> >> > Const ForReading = 1
>> >> > Const ForWriting = 2
>> >> >
>> >> > Set objFSO = CreateObject("Scripting.FileSystemObject")
>> >> > Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
>> >> >
>> >> > strText = objFile.ReadAll
>> >> > objFile.Close
>> >> > strNewText = Replace(strText, "Jim ", "James ")
>> >> > ****strNewText = Replace(strText, "abc ", "apple ")
>> >> >
>> >> > Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
>> >> > objFile.WriteLine strNewText
>> >> > objFile.Close
>> >>
>> >> You probably already have file script56.chm on your computer. If not,
>> >> go
>> >> to
>> >> msdn.microsoft.com's download tab and search for and download it, and
>> >> read
>> >> up on the replace function.
>> >>
>> >> It says that there are potentially more parameters for the replace
>> >> function:
>> >> Replace Function
>> >> See Also
>> >> Filter Function
>> >> Requirements
>> >> Version 2
>> >> Returns a string in which a specified substring has been replaced with
>> >> another substring a specified number of times.
>> >>
>> >> Replace(expression, find, replacewith[, start[, count[, compare]]])
>> >> Arguments
>> >> expression
>> >> Required. String expression containing substring to replace.
>> >> find
>> >> Required. Substring being searched for.
>> >> replacewith
>> >> Required. Replacement substring.
>> >> start
>> >> Optional. Position within expression where substring search is to
>> >> begin.
>> >> If
>> >> omitted, 1 is assumed. Must be used in conjunction with count.
>> >> count
>> >> Optional. Number of substring substitutions to perform. If omitted,
>> >> the
>> >> default value is -1, which means make all possible substitutions. Must
>> >> be
>> >> used in conjunction with start.
>> >> compare
>> >> Optional. Numeric value indicating the kind of comparison to use when
>> >> evaluating substrings. See Settings section for values. If omitted,
>> >> the
>> >> default value is 0, which means perform a binary comparison.
>> >>
>> >> -Paul Randall
>> >>
>> >>
>> >>
>>
>>
>>

My System SpecsSystem Spec
Old 06-15-2009   #9 (permalink)
ekkehard.horner


 
 

Re: script for multi text replacement

tonycd schrieb:
[...]
Quote:

> But how can i enhance the script for multi - text replacement.
[...]
To make it easy to experiment with the aspects pointed out by
the other contributors (and to add an array/loop based approach):

demo script:

Dim sSrc : sSrc = "one two three ONE TWO THREE one two three"
Dim sTrg

' Replace( expression, find, replacewith [, start (1) [, count (-1) [, compare
(vbBinaryCompare) ) ] ] ] )

sTrg = Replace( sSrc, "one" , "eins" )
sTrg = Replace( sTrg, "two" , "zwei" )
sTrg = Replace( sTrg, "three", "drei" )
WScript.Echo Join( Array( "sequential repl, all, honor case", sSrc, sTrg, "" ), vbCrLf )

sTrg = Replace( sSrc, "one" , "eins", 1, -1, vbTextCompare )
sTrg = Replace( sTrg, "two" , "zwei", 1, -1, vbTextCompare )
sTrg = Replace( sTrg, "three", "drei", 1, -1, vbTextCompare )
WScript.Echo Join( Array( "sequential repl, all, ignore case", sSrc, sTrg, "" ), vbCrLf )

sTrg = Replace( sSrc, "one" , "eins", 1, 1, vbBinaryCompare )
sTrg = Replace( sTrg, "two" , "zwei", 1, 1, vbBinaryCompare )
sTrg = Replace( sTrg, "three", "drei", 1, 1, vbBinaryCompare )
WScript.Echo Join( Array( "sequential repl, just one, ignore case", sSrc, sTrg, "" ),
vbCrLf )

sTrg = Replace( Replace( Replace( sSrc, "one", "eins" ), "two", "zwei" ), "three", "drei" )
WScript.Echo Join( Array( "nested repl, all, honor case", sSrc, sTrg, "" ), vbCrLf )

sTrg = Replace( Replace( Replace( sSrc, "one", "eins", 1, 2, vbTextCompare ), "two",
"zwei", 1, 2, vbTextCompare ), "three", "drei", 1, 2, vbTextCompare )
WScript.Echo Join( Array( "nested repl, just two, ignore case", sSrc, sTrg, "" ), vbCrLf )

Dim aRpl( 1, 2 )
Dim nRpl
aRpl( 0, 0 ) = "one"
aRpl( 1, 0 ) = "eins"
aRpl( 0, 1 ) = "two"
aRpl( 1, 1 ) = "zwei"
aRpl( 0, 2 ) = "three"
aRpl( 1, 2 ) = "drei"

sTrg = sSrc
For nRpl = 0 To UBound( aRpl, 2 )
sTrg = Replace( sTrg, aRpl( 0, nRpl ), aRpl( 1, nRpl ) )
Next
WScript.Echo Join( Array( "looped repl, all, honor case", sSrc, sTrg, "" ), vbCrLf )

sTrg = sSrc
For nRpl = 0 To UBound( aRpl, 2 )
sTrg = Replace( sTrg, aRpl( 0, nRpl ), aRpl( 1, nRpl ), 1, -1, vbTextCompare )
Next
WScript.Echo Join( Array( "looped repl, all, ignore case", sSrc, sTrg, "" ), vbCrLf )

sTrg = replaceAll( sSrc, aRpl )
WScript.Echo Join( Array( "repl func, all, honor case", sSrc, sTrg, "" ), vbCrLf )

sTrg = replaceAllP( sSrc, aRpl, 2, vbTextCompare )
WScript.Echo Join( Array( "repl func parm, just two, ignore case", sSrc, sTrg, "" ),
vbCrLf )

sTrg = Replace( sSrc, "one" , "eins", 5, -1, vbTextCompare )
WScript.Echo Join( Array( "one repl, all, ignore case, starting at 5", sSrc, sTrg, ""
), vbCrLf )

Function replaceAll( sSrc, aRpl )
replaceAll = sSrc
Dim nRpl
For nRpl = 0 To UBound( aRpl, 2 )
replaceAll = Replace( replaceAll, aRpl( 0, nRpl ), aRpl( 1, nRpl ) )
Next
End Function

Function replaceAllP( sSrc, aRpl, nCount, vbComp )
replaceAllP = sSrc
Dim nRpl
For nRpl = 0 To UBound( aRpl, 2 )
replaceAllP = Replace( replaceAllP, aRpl( 0, nRpl ), aRpl( 1, nRpl ), 1, nCount,
vbComp )
Next
End Function

output:

=== replDemo04: Replace Demo (04) =============================
sequential repl, all, honor case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE eins zwei drei

sequential repl, all, ignore case
one two three ONE TWO THREE one two three
eins zwei drei eins zwei drei eins zwei drei

sequential repl, just one, ignore case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE one two three

nested repl, all, honor case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE eins zwei drei

nested repl, just two, ignore case
one two three ONE TWO THREE one two three
eins zwei drei eins zwei drei one two three

looped repl, all, honor case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE eins zwei drei

looped repl, all, ignore case
one two three ONE TWO THREE one two three
eins zwei drei eins zwei drei eins zwei drei

repl func, all, honor case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE eins zwei drei

repl func parm, just two, ignore case
one two three ONE TWO THREE one two three
eins zwei drei eins zwei drei one two three

one repl, all, ignore case, starting at 5
one two three ONE TWO THREE one two three
two three eins TWO THREE eins two three

=== replDemo04: 0 done (00:00:00) =============================

The last test case is included to show that using the start
parameter should be avoided in multi-text replacements.

My System SpecsSystem Spec
Old 06-16-2009   #10 (permalink)
tonycd


 
 

Re: script for multi text replacement

Thanks All

I can't retrieve the script56.chm after download. (no contect at the right
hand side).
Would i use simple vbs instead as below. Thanks

dim a
dim b
dim c
document.write(Replace(a,"538","logoff"))
document.write(Replace(b,"540","logon"))
document.write(Replace(b,"528","logon"))


"ekkehard.horner" wrote:
Quote:

> tonycd schrieb:
> [...]
Quote:

> > But how can i enhance the script for multi - text replacement.
> [...]
> To make it easy to experiment with the aspects pointed out by
> the other contributors (and to add an array/loop based approach):
>
> demo script:
>
> Dim sSrc : sSrc = "one two three ONE TWO THREE one two three"
> Dim sTrg
>
> ' Replace( expression, find, replacewith [, start (1) [, count (-1) [, compare
> (vbBinaryCompare) ) ] ] ] )
>
> sTrg = Replace( sSrc, "one" , "eins" )
> sTrg = Replace( sTrg, "two" , "zwei" )
> sTrg = Replace( sTrg, "three", "drei" )
> WScript.Echo Join( Array( "sequential repl, all, honor case", sSrc, sTrg, "" ), vbCrLf )
>
> sTrg = Replace( sSrc, "one" , "eins", 1, -1, vbTextCompare )
> sTrg = Replace( sTrg, "two" , "zwei", 1, -1, vbTextCompare )
> sTrg = Replace( sTrg, "three", "drei", 1, -1, vbTextCompare )
> WScript.Echo Join( Array( "sequential repl, all, ignore case", sSrc, sTrg, "" ), vbCrLf )
>
> sTrg = Replace( sSrc, "one" , "eins", 1, 1, vbBinaryCompare )
> sTrg = Replace( sTrg, "two" , "zwei", 1, 1, vbBinaryCompare )
> sTrg = Replace( sTrg, "three", "drei", 1, 1, vbBinaryCompare )
> WScript.Echo Join( Array( "sequential repl, just one, ignore case", sSrc, sTrg, "" ),
> vbCrLf )
>
> sTrg = Replace( Replace( Replace( sSrc, "one", "eins" ), "two", "zwei" ), "three", "drei" )
> WScript.Echo Join( Array( "nested repl, all, honor case", sSrc, sTrg, "" ), vbCrLf )
>
> sTrg = Replace( Replace( Replace( sSrc, "one", "eins", 1, 2, vbTextCompare ), "two",
> "zwei", 1, 2, vbTextCompare ), "three", "drei", 1, 2, vbTextCompare )
> WScript.Echo Join( Array( "nested repl, just two, ignore case", sSrc, sTrg, "" ), vbCrLf )
>
> Dim aRpl( 1, 2 )
> Dim nRpl
> aRpl( 0, 0 ) = "one"
> aRpl( 1, 0 ) = "eins"
> aRpl( 0, 1 ) = "two"
> aRpl( 1, 1 ) = "zwei"
> aRpl( 0, 2 ) = "three"
> aRpl( 1, 2 ) = "drei"
>
> sTrg = sSrc
> For nRpl = 0 To UBound( aRpl, 2 )
> sTrg = Replace( sTrg, aRpl( 0, nRpl ), aRpl( 1, nRpl ) )
> Next
> WScript.Echo Join( Array( "looped repl, all, honor case", sSrc, sTrg, "" ), vbCrLf )
>
> sTrg = sSrc
> For nRpl = 0 To UBound( aRpl, 2 )
> sTrg = Replace( sTrg, aRpl( 0, nRpl ), aRpl( 1, nRpl ), 1, -1, vbTextCompare )
> Next
> WScript.Echo Join( Array( "looped repl, all, ignore case", sSrc, sTrg, "" ), vbCrLf )
>
> sTrg = replaceAll( sSrc, aRpl )
> WScript.Echo Join( Array( "repl func, all, honor case", sSrc, sTrg, "" ), vbCrLf )
>
> sTrg = replaceAllP( sSrc, aRpl, 2, vbTextCompare )
> WScript.Echo Join( Array( "repl func parm, just two, ignore case", sSrc, sTrg, "" ),
> vbCrLf )
>
> sTrg = Replace( sSrc, "one" , "eins", 5, -1, vbTextCompare )
> WScript.Echo Join( Array( "one repl, all, ignore case, starting at 5", sSrc, sTrg, ""
> ), vbCrLf )
>
> Function replaceAll( sSrc, aRpl )
> replaceAll = sSrc
> Dim nRpl
> For nRpl = 0 To UBound( aRpl, 2 )
> replaceAll = Replace( replaceAll, aRpl( 0, nRpl ), aRpl( 1, nRpl ) )
> Next
> End Function
>
> Function replaceAllP( sSrc, aRpl, nCount, vbComp )
> replaceAllP = sSrc
> Dim nRpl
> For nRpl = 0 To UBound( aRpl, 2 )
> replaceAllP = Replace( replaceAllP, aRpl( 0, nRpl ), aRpl( 1, nRpl ), 1, nCount,
> vbComp )
> Next
> End Function
>
> output:
>
> === replDemo04: Replace Demo (04) =============================
> sequential repl, all, honor case
> one two three ONE TWO THREE one two three
> eins zwei drei ONE TWO THREE eins zwei drei
>
> sequential repl, all, ignore case
> one two three ONE TWO THREE one two three
> eins zwei drei eins zwei drei eins zwei drei
>
> sequential repl, just one, ignore case
> one two three ONE TWO THREE one two three
> eins zwei drei ONE TWO THREE one two three
>
> nested repl, all, honor case
> one two three ONE TWO THREE one two three
> eins zwei drei ONE TWO THREE eins zwei drei
>
> nested repl, just two, ignore case
> one two three ONE TWO THREE one two three
> eins zwei drei eins zwei drei one two three
>
> looped repl, all, honor case
> one two three ONE TWO THREE one two three
> eins zwei drei ONE TWO THREE eins zwei drei
>
> looped repl, all, ignore case
> one two three ONE TWO THREE one two three
> eins zwei drei eins zwei drei eins zwei drei
>
> repl func, all, honor case
> one two three ONE TWO THREE one two three
> eins zwei drei ONE TWO THREE eins zwei drei
>
> repl func parm, just two, ignore case
> one two three ONE TWO THREE one two three
> eins zwei drei eins zwei drei one two three
>
> one repl, all, ignore case, starting at 5
> one two three ONE TWO THREE one two three
> two three eins TWO THREE eins two three
>
> === replDemo04: 0 done (00:00:00) =============================
>
> The last test case is included to show that using the start
> parameter should be avoided in multi-text replacements.
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Vista Logon Script Replacement VB Script
VB Script that searches a file for different text, with an append. VB Script
output .ps1 script to text file PowerShell
Multi-line text becomes a one-liner PowerShell
Multi-task in script PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46