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 - Newbie question: replace multiple strings in multiple text files

Reply
 
Old 10-26-2008   #1 (permalink)
FenderAxe


 
 

Newbie question: replace multiple strings in multiple text files

Hi there --

I've never created a script before now, and what I want to do is
periodically have a script go into fifteen text files and do multiple
search and replaces in each file. Ideally this would happen automatically,
such as one time per day.

I tested the example code from
http://www.microsoft.com/technet/scr.../feb05/hey0208
..mspx (Hey Scripting Guy) that performs one search and replace in one text
file:

*******Begin example*********

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 ")

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

*******End example*********

This works fine for my purposes, however I need the script to perform 15
search and replace operations on the file.

Can I accomplish this just by repeating the following line with different
values?::

strNewText = Replace(strText, "Jim ", "James ")

Or is there something else I need to do to have multiple replace operations
performed?

Thanks for any advice you can provide.

FA



My System SpecsSystem Spec
Old 10-26-2008   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: Newbie question: replace multiple strings in multiple text files

See comment inline below:

"FenderAxe" <fa@xxxxxx> wrote in message
news:Xns9B439FA1360EAfaaxecom@xxxxxx
Quote:

> Hi there --
>
> I've never created a script before now, and what I want to do is
> periodically have a script go into fifteen text files and do multiple
> search and replaces in each file. Ideally this would happen automatically,
> such as one time per day.
>
> I tested the example code from
> http://www.microsoft.com/technet/scr.../feb05/hey0208
> .mspx (Hey Scripting Guy) that performs one search and replace in one text
> file:
>
> *******Begin example*********
>
> 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 ")
>
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
> objFile.WriteLine strNewText
> objFile.Close
>
> *******End example*********
>
> This works fine for my purposes, however I need the script to perform 15
> search and replace operations on the file.
>
> Can I accomplish this just by repeating the following line with different
> values?::
>
> strNewText = Replace(strText, "Jim ", "James ")
Yes. You can have as many Replace statements as desired.
Quote:

>
> Or is there something else I need to do to have multiple replace
> operations
> performed?
>
> Thanks for any advice you can provide.
>
> FA
>
>
If you have 15 files, you will need to open and close each separately. This
can happen in one script. To happen automatically you will need to schedule
the script in Task Scheduler. The script must run with credentials that have
write permissions on the files.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 10-26-2008   #3 (permalink)
Pegasus \(MVP\)


 
 

Re: Newbie question: replace multiple strings in multiple text files


"FenderAxe" <fa@xxxxxx> wrote in message
news:Xns9B439FA1360EAfaaxecom@xxxxxx
Quote:

> Hi there --
>
> I've never created a script before now, and what I want to do is
> periodically have a script go into fifteen text files and do multiple
> search and replaces in each file. Ideally this would happen automatically,
> such as one time per day.
>
> I tested the example code from
> http://www.microsoft.com/technet/scr.../feb05/hey0208
> .mspx (Hey Scripting Guy) that performs one search and replace in one text
> file:
>
> *******Begin example*********
>
> 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 ")
>
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
> objFile.WriteLine strNewText
> objFile.Close
>
> *******End example*********
>
> This works fine for my purposes, however I need the script to perform 15
> search and replace operations on the file.
>
> Can I accomplish this just by repeating the following line with different
> values?::
>
> strNewText = Replace(strText, "Jim ", "James ")
>
> Or is there something else I need to do to have multiple replace
> operations
> performed?
>
> Thanks for any advice you can provide.
>
> FA
Welcome back!
You could do it like so:

Const ForReading = 1, ForWriting = 2
Const Start = 1, Count = - 1

arrFrom = Split("Jim Jack John Jill")
arrTo = Split("James Jake Jack Jane")

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

strText = objFile.ReadAll
objFile.Close
For i = 0 To UBound(arrFrom)
strText = Replace(strText, arrFrom(i), arrTo(i), _
Start, Count, vbTextCompare)
Next

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


My System SpecsSystem Spec
Old 10-26-2008   #4 (permalink)
FenderAxe


 
 

Re: Newbie question: replace multiple strings in multiple text files

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
news:O2RTnC8NJHA.3676@xxxxxx:
Quote:

> See comment inline below:
>
> "FenderAxe" <fa@xxxxxx> wrote in message
> news:Xns9B439FA1360EAfaaxecom@xxxxxx
Quote:

>> Hi there --
>>
>> I've never created a script before now, and what I want to do is
>> periodically have a script go into fifteen text files and do multiple
>> search and replaces in each file. Ideally this would happen
>> automatically, such as one time per day.
>>
>> I tested the example code from
>> http://www.microsoft.com/technet/scr...qanda/feb05/he
>> y0208 .mspx (Hey Scripting Guy) that performs one search and replace
>> in one text file:
>>
>> *******Begin example*********
>>
>> 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 ")
>>
>> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
>> objFile.WriteLine strNewText
>> objFile.Close
>>
>> *******End example*********
>>
>> This works fine for my purposes, however I need the script to perform
>> 15 search and replace operations on the file.
>>
>> Can I accomplish this just by repeating the following line with
>> different values?::
>>
>> strNewText = Replace(strText, "Jim ", "James ")
>
> Yes. You can have as many Replace statements as desired.
>
Quote:

>>
>> Or is there something else I need to do to have multiple replace
>> operations
>> performed?
>>
>> Thanks for any advice you can provide.
>>
>> FA
>>
>>
>
> If you have 15 files, you will need to open and close each separately.
> This can happen in one script. To happen automatically you will need
> to schedule the script in Task Scheduler. The script must run with
> credentials that have write permissions on the files.
>
I gave this a try with the strNewText line modified and then repeated as
follows:


*****Begin script*****

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
ForReading)

strText = objFile.ReadAll
objFile.Close

strNewText = Replace(strText, "J:\Music\Blues", "H:\Media2\Blues")
strNewText = Replace(strText, "J:\Music\Classical", "G:\Media2\Classical")
strNewText = Replace(strText, "J:\Music\Country", "G:\Media2\Country")
strNewText = Replace(strText, "J:\Music\Disco", "G:\Media2\Disco")
strNewText = Replace(strText, "J:\Music\Folk", "G:\Media2\Folk")
strNewText = Replace(strText, "J:\Music\Jazz", "G:\Media2\Jazz")
strNewText = Replace(strText, "J:\Music\Misc", "G:\Media2\Misc")
strNewText = Replace(strText, "J:\Music\Pop", "G:\Media2\Pop")
strNewText = Replace(strText, "J:\Music\R and B", "G:\Media2\R and B")
strNewText = Replace(strText, "J:\Music\Reggae", "G:\Media2\Reggae")
strNewText = Replace(strText, "J:\Music\Rock A-C", "H:\Media\Rock A-C")
strNewText = Replace(strText, "J:\Music\Rock D-G","H:\Media\Rock D-G")
strNewText = Replace(strText, "J:\Music\Rock H-M", "H:\Media\Rock H-M")
strNewText = Replace(strText, "J:\Music\Rock N-P", "H:\Media\Rock N-P")
strNewText = Replace(strText, "J:\Music\Rock Q-S", "H:\Media\Rock Q-S")
strNewText = Replace(strText, "J:\Music\Rock T-Z", "H:\Media\Rock T-Z")
strNewText = Replace(strText, "J:\Music\Soundtrack",
"G:\Media2\Soundtrack")
strNewText = Replace(strText, "J:\Music\Xmas", "G:\Media2\Xmas")
strNewText = Replace(strText, "J:\Music\Zydeco", "G:\Media2\Zydeco")

Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
ForWriting)
objFile.WriteLine strNewText
objFile.Close

*****End script*****

Unfortunately, this didn't work -- the playlist m3u file was not updated,
although I did not receive any error messages at the command prompt.

The command that I ran and results I recieved at the prompt was:

C:\Users\Jrm>cscript I:\Documents\WSH\playlistfix.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Interestingly, the script works when I only have one "strNewText" item in
place.

FA
My System SpecsSystem Spec
Old 10-26-2008   #5 (permalink)
Todd Vargo


 
 

Re: Newbie question: replace multiple strings in multiple text files


"FenderAxe" <fa@xxxxxx> wrote in message
news:Xns9B439FA1360EAfaaxecom@xxxxxx
Quote:

> Hi there --
>
> I've never created a script before now, and what I want to do is
> periodically have a script go into fifteen text files and do multiple
> search and replaces in each file. Ideally this would happen automatically,
> such as one time per day.
>
> I tested the example code from
>
http://www.microsoft.com/technet/scr.../feb05/hey0208
Quote:

> .mspx (Hey Scripting Guy) that performs one search and replace in one text
> file:
>
> *******Begin example*********
>
> 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 ")
>
> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
> objFile.WriteLine strNewText
> objFile.Close
>
> *******End example*********
>
> This works fine for my purposes, however I need the script to perform 15
> search and replace operations on the file.
>
> Can I accomplish this just by repeating the following line with different
> values?::
>
> strNewText = Replace(strText, "Jim ", "James ")
>
> Or is there something else I need to do to have multiple replace
operations
Quote:

> performed?
>
> Thanks for any advice you can provide.
Since your first replacement is from strText to strNewText, subsequent
replacements can be from strNewText to strNewText. The final result will
have all of the replacements in strNewText prior to writing back to file.

strNewText = Replace(strText, "Jim ", "James ")
strNewText = Replace(strNewText, "Mike ", "Miles ")
strNewText = Replace(strNewText, "Guitar ", "Axe ")

You can load these values into an array as other posters demonstrate. The
same method can be applied to the file names but you can just as easily
provide the filenames to a subroutine.

Update "C:\Scripts\Text.txt"
Update "C:\Other Location\Text1.txt"
Update "C:\Yet Another\Text2.txt"
'and so on...

Sub Update(filename)
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(filename, ForReading)
strText = objFile.ReadAll
objFile.Close

strNewText = Replace(strText, "Jim ", "James ")
strNewText = Replace(strNewText, "Mike ", "Miles ")
strNewText = Replace(strNewText, "FenderGuitar ", "FenderAxe ")
'and so on...

Set objFile = objFSO.OpenTextFile(filename, ForWriting)
objFile.Write strNewText
objFile.Close
End Sub

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 10-26-2008   #6 (permalink)
Richard Mueller [MVP]


 
 

Re: Newbie question: replace multiple strings in multiple text files


"FenderAxe" <fa@xxxxxx> wrote in message
news:Xns9B43AFD98D9C1faaxecom@xxxxxx
Quote:

> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
> news:O2RTnC8NJHA.3676@xxxxxx:
>
Quote:

>> See comment inline below:
>>
>> "FenderAxe" <fa@xxxxxx> wrote in message
>> news:Xns9B439FA1360EAfaaxecom@xxxxxx
Quote:

>>> Hi there --
>>>
>>> I've never created a script before now, and what I want to do is
>>> periodically have a script go into fifteen text files and do multiple
>>> search and replaces in each file. Ideally this would happen
>>> automatically, such as one time per day.
>>>
>>> I tested the example code from
>>> http://www.microsoft.com/technet/scr...qanda/feb05/he
>>> y0208 .mspx (Hey Scripting Guy) that performs one search and replace
>>> in one text file:
>>>
>>> *******Begin example*********
>>>
>>> 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 ")
>>>
>>> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
>>> objFile.WriteLine strNewText
>>> objFile.Close
>>>
>>> *******End example*********
>>>
>>> This works fine for my purposes, however I need the script to perform
>>> 15 search and replace operations on the file.
>>>
>>> Can I accomplish this just by repeating the following line with
>>> different values?::
>>>
>>> strNewText = Replace(strText, "Jim ", "James ")
>>
>> Yes. You can have as many Replace statements as desired.
>>
Quote:

>>>
>>> Or is there something else I need to do to have multiple replace
>>> operations
>>> performed?
>>>
>>> Thanks for any advice you can provide.
>>>
>>> FA
>>>
>>>
>>
>> If you have 15 files, you will need to open and close each separately.
>> This can happen in one script. To happen automatically you will need
>> to schedule the script in Task Scheduler. The script must run with
>> credentials that have write permissions on the files.
>>
>
> I gave this a try with the strNewText line modified and then repeated as
> follows:
>
>
> *****Begin script*****
>
> Const ForReading = 1
> Const ForWriting = 2
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
> ForReading)
>
> strText = objFile.ReadAll
> objFile.Close
>
> strNewText = Replace(strText, "J:\Music\Blues", "H:\Media2\Blues")
> strNewText = Replace(strText, "J:\Music\Classical", "G:\Media2\Classical")
> strNewText = Replace(strText, "J:\Music\Country", "G:\Media2\Country")
> strNewText = Replace(strText, "J:\Music\Disco", "G:\Media2\Disco")
> strNewText = Replace(strText, "J:\Music\Folk", "G:\Media2\Folk")
> strNewText = Replace(strText, "J:\Music\Jazz", "G:\Media2\Jazz")
> strNewText = Replace(strText, "J:\Music\Misc", "G:\Media2\Misc")
> strNewText = Replace(strText, "J:\Music\Pop", "G:\Media2\Pop")
> strNewText = Replace(strText, "J:\Music\R and B", "G:\Media2\R and B")
> strNewText = Replace(strText, "J:\Music\Reggae", "G:\Media2\Reggae")
> strNewText = Replace(strText, "J:\Music\Rock A-C", "H:\Media\Rock A-C")
> strNewText = Replace(strText, "J:\Music\Rock D-G","H:\Media\Rock D-G")
> strNewText = Replace(strText, "J:\Music\Rock H-M", "H:\Media\Rock H-M")
> strNewText = Replace(strText, "J:\Music\Rock N-P", "H:\Media\Rock N-P")
> strNewText = Replace(strText, "J:\Music\Rock Q-S", "H:\Media\Rock Q-S")
> strNewText = Replace(strText, "J:\Music\Rock T-Z", "H:\Media\Rock T-Z")
> strNewText = Replace(strText, "J:\Music\Soundtrack",
> "G:\Media2\Soundtrack")
> strNewText = Replace(strText, "J:\Music\Xmas", "G:\Media2\Xmas")
> strNewText = Replace(strText, "J:\Music\Zydeco", "G:\Media2\Zydeco")
>
> Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
> ForWriting)
> objFile.WriteLine strNewText
> objFile.Close
>
> *****End script*****
>
> Unfortunately, this didn't work -- the playlist m3u file was not updated,
> although I did not receive any error messages at the command prompt.
>
> The command that I ran and results I recieved at the prompt was:
>
> C:\Users\Jrm>cscript I:\Documents\WSH\playlistfix.vbs
> Microsoft (R) Windows Script Host Version 5.7
> Copyright (C) Microsoft Corporation. All rights reserved.
>
> Interestingly, the script works when I only have one "strNewText" item in
> place.
>
> FA
You redefine the value of strNewText each time based on the original value
of strText. Previous modified values are lost. So only the last Replace is
used. You should use:
===========
strText = objFile.ReadAll
objFile.Close

strNewText = Replace(strText, "J:\Music\Blues", "H:\Media2\Blues")
strNewText = Replace(strNewText, "J:\Music\Classical",
"G:\Media2\Classical")
strNewText = Replace(strNewText, "J:\Music\Country", "G:\Media2\Country")
strNewText = Replace(strNewText, "J:\Music\Disco", "G:\Media2\Disco")
strNewText = Replace(strNewText, "J:\Music\Folk", "G:\Media2\Folk")
strNewText = Replace(strNewText, "J:\Music\Jazz", "G:\Media2\Jazz")
strNewText = Replace(strNewText, "J:\Music\Misc", "G:\Media2\Misc")
strNewText = Replace(strNewText, "J:\Music\Pop", "G:\Media2\Pop")
strNewText = Replace(strNewText, "J:\Music\R and B", "G:\Media2\R and B")
strNewText = Replace(strNewText, "J:\Music\Reggae", "G:\Media2\Reggae")
strNewText = Replace(strNewText, "J:\Music\Rock A-C", "H:\Media\Rock A-C")
strNewText = Replace(strNewText, "J:\Music\Rock D-G","H:\Media\Rock D-G")
strNewText = Replace(strNewText, "J:\Music\Rock H-M", "H:\Media\Rock H-M")
strNewText = Replace(strNewText, "J:\Music\Rock N-P", "H:\Media\Rock N-P")
strNewText = Replace(strNewText, "J:\Music\Rock Q-S", "H:\Media\Rock Q-S")
strNewText = Replace(strNewText, "J:\Music\Rock T-Z", "H:\Media\Rock T-Z")
strNewText = Replace(strNewText, "J:\Music\Soundtrack",
"G:\Media2\Soundtrack")
strNewText = Replace(strNewText, "J:\Music\Xmas", "G:\Media2\Xmas")
strNewText = Replace(strNewText, "J:\Music\Zydeco", "G:\Media2\Zydeco")

Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
ForWriting)
objFile.WriteLine strNewText
objFile.Close
========
Now the modifications are cumulative. Does this make sense?

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 10-26-2008   #7 (permalink)
FenderAxe


 
 

Re: Newbie question: replace multiple strings in multiple text files

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
news:#yihVP9NJHA.1744@xxxxxx:
Quote:

>
> "FenderAxe" <fa@xxxxxx> wrote in message
> news:Xns9B43AFD98D9C1faaxecom@xxxxxx
Quote:

>> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote
>> in news:O2RTnC8NJHA.3676@xxxxxx:
>>
Quote:

>>> See comment inline below:
>>>
>>> "FenderAxe" <fa@xxxxxx> wrote in message
>>> news:Xns9B439FA1360EAfaaxecom@xxxxxx
>>>> Hi there --
>>>>
>>>> I've never created a script before now, and what I want to do is
>>>> periodically have a script go into fifteen text files and do
>>>> multiple search and replaces in each file. Ideally this would
>>>> happen automatically, such as one time per day.
>>>>
>>>> I tested the example code from
>>>> http://www.microsoft.com/technet/scr...s/qanda/feb05/
>>>> he y0208 .mspx (Hey Scripting Guy) that performs one search and
>>>> replace in one text file:
>>>>
>>>> *******Begin example*********
>>>>
>>>> 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 ")
>>>>
>>>> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt",
>>>> ForWriting) objFile.WriteLine strNewText
>>>> objFile.Close
>>>>
>>>> *******End example*********
>>>>
>>>> This works fine for my purposes, however I need the script to
>>>> perform 15 search and replace operations on the file.
>>>>
>>>> Can I accomplish this just by repeating the following line with
>>>> different values?::
>>>>
>>>> strNewText = Replace(strText, "Jim ", "James ")
>>>
>>> Yes. You can have as many Replace statements as desired.
>>>
>>>>
>>>> Or is there something else I need to do to have multiple replace
>>>> operations
>>>> performed?
>>>>
>>>> Thanks for any advice you can provide.
>>>>
>>>> FA
>>>>
>>>>
>>>
>>> If you have 15 files, you will need to open and close each
>>> separately. This can happen in one script. To happen automatically
>>> you will need to schedule the script in Task Scheduler. The script
>>> must run with credentials that have write permissions on the files.
>>>
>>
>> I gave this a try with the strNewText line modified and then
>> repeated as follows:
>>
>>
>> *****Begin script*****
>>
>> Const ForReading = 1
>> Const ForWriting = 2
>>
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and
>> Faves.m3u", ForReading)
>>
>> strText = objFile.ReadAll
>> objFile.Close
>>
>> strNewText = Replace(strText, "J:\Music\Blues", "H:\Media2\Blues")
>> strNewText = Replace(strText, "J:\Music\Classical",
>> "G:\Media2\Classical") strNewText = Replace(strText,
>> "J:\Music\Country", "G:\Media2\Country") strNewText =
>> Replace(strText, "J:\Music\Disco", "G:\Media2\Disco") strNewText =
>> Replace(strText, "J:\Music\Folk", "G:\Media2\Folk") strNewText =
>> Replace(strText, "J:\Music\Jazz", "G:\Media2\Jazz") strNewText =
>> Replace(strText, "J:\Music\Misc", "G:\Media2\Misc") strNewText =
>> Replace(strText, "J:\Music\Pop", "G:\Media2\Pop") strNewText =
>> Replace(strText, "J:\Music\R and B", "G:\Media2\R and B") strNewText
>> = Replace(strText, "J:\Music\Reggae", "G:\Media2\Reggae") strNewText
>> = Replace(strText, "J:\Music\Rock A-C", "H:\Media\Rock A-C")
>> strNewText = Replace(strText, "J:\Music\Rock D-G","H:\Media\Rock
>> D-G") strNewText = Replace(strText, "J:\Music\Rock H-M",
>> "H:\Media\Rock H-M") strNewText = Replace(strText, "J:\Music\Rock
>> N-P", "H:\Media\Rock N-P") strNewText = Replace(strText,
>> "J:\Music\Rock Q-S", "H:\Media\Rock Q-S") strNewText =
>> Replace(strText, "J:\Music\Rock T-Z", "H:\Media\Rock T-Z") strNewText
>> = Replace(strText, "J:\Music\Soundtrack", "G:\Media2\Soundtrack")
>> strNewText = Replace(strText, "J:\Music\Xmas", "G:\Media2\Xmas")
>> strNewText = Replace(strText, "J:\Music\Zydeco", "G:\Media2\Zydeco")
>>
>> Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and
>> Faves.m3u", ForWriting)
>> objFile.WriteLine strNewText
>> objFile.Close
>>
>> *****End script*****
>>
>> Unfortunately, this didn't work -- the playlist m3u file was not
>> updated, although I did not receive any error messages at the command
>> prompt.
>>
>> The command that I ran and results I recieved at the prompt was:
>>
>> C:\Users\Jrm>cscript I:\Documents\WSH\playlistfix.vbs
>> Microsoft (R) Windows Script Host Version 5.7
>> Copyright (C) Microsoft Corporation. All rights reserved.
>>
>> Interestingly, the script works when I only have one "strNewText"
>> item in place.
>>
>> FA
>
> You redefine the value of strNewText each time based on the original
> value of strText. Previous modified values are lost. So only the last
> Replace is used. You should use:
> ===========
> strText = objFile.ReadAll
> objFile.Close
>
> strNewText = Replace(strText, "J:\Music\Blues", "H:\Media2\Blues")
> strNewText = Replace(strNewText, "J:\Music\Classical",
> "G:\Media2\Classical")
> strNewText = Replace(strNewText, "J:\Music\Country",
> "G:\Media2\Country") strNewText = Replace(strNewText,
> "J:\Music\Disco", "G:\Media2\Disco") strNewText = Replace(strNewText,
> "J:\Music\Folk", "G:\Media2\Folk") strNewText = Replace(strNewText,
> "J:\Music\Jazz", "G:\Media2\Jazz") strNewText = Replace(strNewText,
> "J:\Music\Misc", "G:\Media2\Misc") strNewText = Replace(strNewText,
> "J:\Music\Pop", "G:\Media2\Pop") strNewText = Replace(strNewText,
> "J:\Music\R and B", "G:\Media2\R and B") strNewText =
> Replace(strNewText, "J:\Music\Reggae", "G:\Media2\Reggae") strNewText
> = Replace(strNewText, "J:\Music\Rock A-C", "H:\Media\Rock A-C")
> strNewText = Replace(strNewText, "J:\Music\Rock D-G","H:\Media\Rock
> D-G") strNewText = Replace(strNewText, "J:\Music\Rock H-M",
> "H:\Media\Rock H-M") strNewText = Replace(strNewText, "J:\Music\Rock
> N-P", "H:\Media\Rock N-P") strNewText = Replace(strNewText,
> "J:\Music\Rock Q-S", "H:\Media\Rock Q-S") strNewText =
> Replace(strNewText, "J:\Music\Rock T-Z", "H:\Media\Rock T-Z")
> strNewText = Replace(strNewText, "J:\Music\Soundtrack",
> "G:\Media2\Soundtrack") strNewText = Replace(strNewText,
> "J:\Music\Xmas", "G:\Media2\Xmas") strNewText = Replace(strNewText,
> "J:\Music\Zydeco", "G:\Media2\Zydeco")
>
> Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
> ForWriting)
> objFile.WriteLine strNewText
> objFile.Close
> ========
> Now the modifications are cumulative. Does this make sense?
>
To be honest, it only partly makes sense -- I guess you're creating a new
variable somehow and replacing the value with a new value?

With the script modified this way it worked though. :-) Thanks for your
help with this.

FA
My System SpecsSystem Spec
Old 10-27-2008   #8 (permalink)
FenderAxe


 
 

Re: Newbie question: replace multiple strings in multiple text files

"Todd Vargo" <tlvargo@xxxxxx> wrote in
news:OHp9Yf8NJHA.3480@xxxxxx:
Quote:

>
> "FenderAxe" <fa@xxxxxx> wrote in message
> news:Xns9B439FA1360EAfaaxecom@xxxxxx
Quote:

>> Hi there --
>>
>> I've never created a script before now, and what I want to do is
>> periodically have a script go into fifteen text files and do multiple
>> search and replaces in each file. Ideally this would happen
>> automatically, such as one time per day.
>>
>> I tested the example code from
>>
> http://www.microsoft.com/technet/scr...anda/feb05/hey
> 0208
Quote:

>> .mspx (Hey Scripting Guy) that performs one search and replace in one
>> text file:
>>
>> *******Begin example*********
>>
>> 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 ")
>>
>> Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
>> objFile.WriteLine strNewText
>> objFile.Close
>>
>> *******End example*********
>>
>> This works fine for my purposes, however I need the script to perform
>> 15 search and replace operations on the file.
>>
>> Can I accomplish this just by repeating the following line with
>> different values?::
>>
>> strNewText = Replace(strText, "Jim ", "James ")
>>
>> Or is there something else I need to do to have multiple replace
> operations
Quote:

>> performed?
>>
>> Thanks for any advice you can provide.
>
> Since your first replacement is from strText to strNewText, subsequent
> replacements can be from strNewText to strNewText. The final result
> will have all of the replacements in strNewText prior to writing back
> to file.
>
> strNewText = Replace(strText, "Jim ", "James ")
> strNewText = Replace(strNewText, "Mike ", "Miles ")
> strNewText = Replace(strNewText, "Guitar ", "Axe ")
>
> You can load these values into an array as other posters demonstrate.
> The same method can be applied to the file names but you can just as
> easily provide the filenames to a subroutine.
>
> Update "C:\Scripts\Text.txt"
> Update "C:\Other Location\Text1.txt"
> Update "C:\Yet Another\Text2.txt"
> 'and so on...
>
> Sub Update(filename)
> Const ForReading = 1
> Const ForWriting = 2
> Set objFSO = CreateObject("Scripting.FileSystemObject")
>
> Set objFile = objFSO.OpenTextFile(filename, ForReading)
> strText = objFile.ReadAll
> objFile.Close
>
> strNewText = Replace(strText, "Jim ", "James ")
> strNewText = Replace(strNewText, "Mike ", "Miles ")
> strNewText = Replace(strNewText, "FenderGuitar ", "FenderAxe ")
> 'and so on...
>
> Set objFile = objFSO.OpenTextFile(filename, ForWriting)
> objFile.Write strNewText
> objFile.Close
> End Sub
>
Hi Todd --

I tried this with the list of files at the top:

Update "C:\Scripts\Text.txt"
Update "C:\Other Location\Text1.txt"
Update "C:\Yet Another\Text2.txt"
'and so on...

: But when I ran the script I received an error that stated "Disk not
ready." This didn't really make sense because the disk was powered up and
running fine. So I'm not sure what the problem is -- but I appreciate your
suggestion anyhow, because I'm learning more with each post you folks
provide, and I appreciate that a lot.

FA
My System SpecsSystem Spec
Old 10-27-2008   #9 (permalink)
Todd Vargo


 
 

Re: Newbie question: replace multiple strings in multiple text files

FenderAxe wrote:
Quote:

> Todd Vargo wrote:
Quote:

> > FenderAxe wrote:
Quote:

> >> Thanks for any advice you can provide.
> >
> > Since your first replacement is from strText to strNewText, subsequent
> > replacements can be from strNewText to strNewText. The final result
> > will have all of the replacements in strNewText prior to writing back
> > to file.
> >
> > strNewText = Replace(strText, "Jim ", "James ")
> > strNewText = Replace(strNewText, "Mike ", "Miles ")
> > strNewText = Replace(strNewText, "Guitar ", "Axe ")
> >
> > You can load these values into an array as other posters demonstrate.
> > The same method can be applied to the file names but you can just as
> > easily provide the filenames to a subroutine.
> >
> > Update "C:\Scripts\Text.txt"
> > Update "C:\Other Location\Text1.txt"
> > Update "C:\Yet Another\Text2.txt"
> > 'and so on...
> >
> > Sub Update(filename)
> > Const ForReading = 1
> > Const ForWriting = 2
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> >
> > Set objFile = objFSO.OpenTextFile(filename, ForReading)
> > strText = objFile.ReadAll
> > objFile.Close
> >
> > strNewText = Replace(strText, "Jim ", "James ")
> > strNewText = Replace(strNewText, "Mike ", "Miles ")
> > strNewText = Replace(strNewText, "FenderGuitar ", "FenderAxe ")
> > 'and so on...
> >
> > Set objFile = objFSO.OpenTextFile(filename, ForWriting)
> > objFile.Write strNewText
> > objFile.Close
> > End Sub
> >
>
> Hi Todd --
>
> I tried this with the list of files at the top:
>
> Update "C:\Scripts\Text.txt"
> Update "C:\Other Location\Text1.txt"
> Update "C:\Yet Another\Text2.txt"
> 'and so on...
>
> : But when I ran the script I received an error that stated "Disk not
> ready." This didn't really make sense because the disk was powered up and
> running fine. So I'm not sure what the problem is -- but I appreciate your
> suggestion anyhow, because I'm learning more with each post you folks
> provide, and I appreciate that a lot.
Why would you try it using my example file specs? You should replace with
your real file specs which you want to modify. Since the "Disk not ready"
message does not make sense, and having read your other post, I suspect you
tried putting the strings to be replaced (which are file paths in the other
post) where the specs to files to be modified should go.

For further help, please copy/paste the exact code that returns the "Disk
not ready" message so we can examine it.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 10-27-2008   #10 (permalink)
Todd Vargo


 
 

Re: Newbie question: replace multiple strings in multiple text files

FenderAxe wrote:
Quote:

> "Richard Mueller [MVP]" wrote:
Quote:

> > You redefine the value of strNewText each time based on the original
> > value of strText. Previous modified values are lost. So only the last
> > Replace is used. You should use:
> > ===========
> > strText = objFile.ReadAll
> > objFile.Close
> >
> > strNewText = Replace(strText, "J:\Music\Blues", "H:\Media2\Blues")
> > strNewText = Replace(strNewText, "J:\Music\Classical",
> > "G:\Media2\Classical")
> > strNewText = Replace(strNewText, "J:\Music\Country",
> > "G:\Media2\Country") strNewText = Replace(strNewText,
> > "J:\Music\Disco", "G:\Media2\Disco") strNewText = Replace(strNewText,
> > "J:\Music\Folk", "G:\Media2\Folk") strNewText = Replace(strNewText,
> > "J:\Music\Jazz", "G:\Media2\Jazz") strNewText = Replace(strNewText,
> > "J:\Music\Misc", "G:\Media2\Misc") strNewText = Replace(strNewText,
> > "J:\Music\Pop", "G:\Media2\Pop") strNewText = Replace(strNewText,
> > "J:\Music\R and B", "G:\Media2\R and B") strNewText =
> > Replace(strNewText, "J:\Music\Reggae", "G:\Media2\Reggae") strNewText
> > = Replace(strNewText, "J:\Music\Rock A-C", "H:\Media\Rock A-C")
> > strNewText = Replace(strNewText, "J:\Music\Rock D-G","H:\Media\Rock
> > D-G") strNewText = Replace(strNewText, "J:\Music\Rock H-M",
> > "H:\Media\Rock H-M") strNewText = Replace(strNewText, "J:\Music\Rock
> > N-P", "H:\Media\Rock N-P") strNewText = Replace(strNewText,
> > "J:\Music\Rock Q-S", "H:\Media\Rock Q-S") strNewText =
> > Replace(strNewText, "J:\Music\Rock T-Z", "H:\Media\Rock T-Z")
> > strNewText = Replace(strNewText, "J:\Music\Soundtrack",
> > "G:\Media2\Soundtrack") strNewText = Replace(strNewText,
> > "J:\Music\Xmas", "G:\Media2\Xmas") strNewText = Replace(strNewText,
> > "J:\Music\Zydeco", "G:\Media2\Zydeco")
> >
> > Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
> > ForWriting)
> > objFile.WriteLine strNewText
> > objFile.Close
> > ========
> > Now the modifications are cumulative. Does this make sense?
> >
>
> To be honest, it only partly makes sense -- I guess you're creating a new
> variable somehow and replacing the value with a new value?
No. His code (like mine) reuses the strNewText variable on the second and
latter Replace() statements.
Quote:

>
> With the script modified this way it worked though. :-) Thanks for your
> help with this.
Richard might have also mentioned, you could replace all those Replace()
lines with just these two lines (in this order).

strNewText = Replace(strText, "J:\Music\Rock", "H:\Media\Rock)
strNewText = Replace(strNewText, "J:\Music", "G:\Media2")

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Replace text in multiple text files VB Script
How to test if string contains one of multiple strings PowerShell
Remove lines from multiple text files PowerShell
find and replace a specific string in multiple files VB Script
replacing text on multiple text files VB Script


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