![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 ") Quote: > > Or is there something else I need to do to have multiple replace > operations > performed? > > Thanks for any advice you can provide. > > FA > > 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 Specs![]() |
| | #3 (permalink) |
| | 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 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 Specs![]() |
| | #4 (permalink) |
| | 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. > 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 Specs![]() |
| | #5 (permalink) |
| | 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 > 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 Quote: > performed? > > Thanks for any advice you can provide. 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 Specs![]() |
| | #6 (permalink) |
| | 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 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 Specs![]() |
| | #7 (permalink) |
| | 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? > 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 Specs![]() |
| | #8 (permalink) |
| | 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 >> > 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 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 > 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 Specs![]() |
| | #9 (permalink) |
| | 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. 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 Specs![]() |
| | #10 (permalink) |
| | 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? latter Replace() statements. Quote: > > With the script modified this way it worked though. :-) Thanks for your > help with this. 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 Specs![]() |
![]() |
| 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 | |||