Re: Newbie question: replace multiple strings in multiple text files
- From: FenderAxe <fa@xxxxxxx>
- Date: 27 Oct 2008 00:16:18 GMT
"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxxxxxxxxxxxxxxxx> wrote in
news:O2RTnC8NJHA.3676@xxxxxxxxxxxxxxxxxxxx:
See comment inline below:
"FenderAxe" <fa@xxxxxxx> wrote in message
news:Xns9B439FA1360EAfaaxecom@xxxxxxxxxxxxxxxxxx
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/scriptcenter/resources/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
.
- Follow-Ups:
- Re: Newbie question: replace multiple strings in multiple text files
- From: Richard Mueller [MVP]
- Re: Newbie question: replace multiple strings in multiple text files
- References:
- Newbie question: replace multiple strings in multiple text files
- From: FenderAxe
- Re: Newbie question: replace multiple strings in multiple text files
- From: Richard Mueller [MVP]
- Newbie question: replace multiple strings in multiple text files
- Prev by Date: Re: question on: HTA and format to include external script file
- Next by Date: Re: Newbie question: replace multiple strings in multiple text files
- Previous by thread: Re: Newbie question: replace multiple strings in multiple text files
- Next by thread: Re: Newbie question: replace multiple strings in multiple text files
- Index(es):
Relevant Pages
|