Newbie question: replace multiple strings in multiple text files


  1.    27 Oct 2008 #11
    Al Dunbar Guest

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



    "Todd Vargo" <tlvargo@xxxxxx> wrote in message
    news:eKkuaNIOJHA.1960@xxxxxx

    > FenderAxe wrote:

    >> "Richard Mueller [MVP]" wrote:

    >> > 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.
    >

    >>
    >> 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")
    .... always assuming that *every* folder within "J:\music" has its
    corresponding folder in "G:\Media2".

    /Al


      My System SpecsSystem Spec

  2.    30 Oct 2008 #12
    FenderAxe Guest

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


    "Todd Vargo" <tlvargo@xxxxxx> wrote in
    news:#95XaNIOJHA.1960@xxxxxx:

    > FenderAxe wrote:

    >> Todd Vargo wrote:

    >> > FenderAxe wrote:
    >> >> 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.
    >
    I didn't try it using example specs, I replaced the values with the actual
    names of my files. I was just indicating that I had added the "Update
    'filename'" section and that because of this the script no longer works.

    Here is the code I've run and received the disk not ready error:

    Update "F:\Documents\Playlists Source\1968 Top 100.m3u"
    Update "F:\Documents\Playlists Source\1972 Top 100.m3u"
    Update "F:\Documents\Playlists Source\1973 Top 100.m3u"
    Update "F:\Documents\Playlists Source\1974 Top 100.m3u"
    Update "F:\Documents\Playlists Source\1975 Top 100.m3u"
    Update "F:\Documents\Playlists Source\1979 Top 100.m3u"
    Update "F:\Documents\Playlists Source\AM Radio.m3u"
    Update "F:\Documents\Playlists Source\Amarcord Nino Rota.m3u"
    Update "F:\Documents\Playlists Source\Audiophile.m3u"
    Update "F:\Documents\Playlists Source\Blues.m3u"
    Update "F:\Documents\Playlists Source\Bs and Faves.m3u"
    Update "F:\Documents\Playlists Source\B's and Faves 2.m3u"
    Update "F:\Documents\Playlists Source\Christmas (short).m3u"
    Update "F:\Documents\Playlists Source\Clapton and Rock.m3u"
    Update "F:\Documents\Playlists Source\Cocktails 1.m3u"
    Update "F:\Documents\Playlists Source\Cocktails 2.m3u"
    Update "F:\Documents\Playlists Source\College.m3u"
    Update "F:\Documents\Playlists Source\Country.m3u"
    Update "F:\Documents\Playlists Source\Dr Winston O Boogie.m3u"
    Update "F:\Documents\Playlists Source\Dylan.m3u"
    Update "F:\Documents\Playlists Source\Eagles.m3u"
    Update "F:\Documents\Playlists Source\ELO and Friends.m3u"
    Update "F:\Documents\Playlists Source\Elvis Costello.m3u"
    Update "F:\Documents\Playlists Source\Guitar Blues.m3u"
    Update "F:\Documents\Playlists Source\Hard Rock 1.m3u"
    Update "F:\Documents\Playlists Source\Hard Rock 2.m3u"
    Update "F:\Documents\Playlists Source\Hendrix.m3u"
    Update "F:\Documents\Playlists Source\Hippy Hippy Shake.m3u"
    Update "F:\Documents\Playlists Source\Jacky Jack.m3u"
    Update "F:\Documents\Playlists Source\James Taylor.m3u"
    Update "F:\Documents\Playlists Source\Jazz - Cool.m3u"
    Update "F:\Documents\Playlists Source\Jazz - Misc.m3u"
    Update "F:\Documents\Playlists Source\Jazz - New.m3u"
    Update "F:\Documents\Playlists Source\Jazz - Smooth.m3u"
    Update "F:\Documents\Playlists Source\Jazz Rock.m3u"
    Update "F:\Documents\Playlists Source\Jazz- Slow.m3u"
    Update "F:\Documents\Playlists Source\JJ and Eric.m3u"
    Update "F:\Documents\Playlists Source\Joni Mitchell.m3u"
    Update "F:\Documents\Playlists Source\Kinks.m3u"
    Update "F:\Documents\Playlists Source\LA.m3u"
    Update "F:\Documents\Playlists Source\Led Zep.m3u"
    Update "F:\Documents\Playlists Source\Lexus.m3u"
    Update "F:\Documents\Playlists Source\Loggins and Messina.m3u"
    Update "F:\Documents\Playlists Source\Maximum 1970's R&B.m3u"
    Update "F:\Documents\Playlists Source\New Orleans.m3u"
    Update "F:\Documents\Playlists Source\Playlist Playlist.m3u"
    Update "F:\Documents\Playlists Source\Pop.m3u"
    Update "F:\Documents\Playlists Source\R and B 1.m3u"
    Update "F:\Documents\Playlists Source\R and B 2.m3u"
    Update "F:\Documents\Playlists Source\RCA Victor Jazz.m3u"
    Update "F:\Documents\Playlists Source\Ricky Nelson.m3u"
    Update "F:\Documents\Playlists Source\Sinatra.m3u"
    Update "F:\Documents\Playlists Source\Smallville.m3u"
    Update "F:\Documents\Playlists Source\Soft Rock 1.m3u"
    Update "F:\Documents\Playlists Source\Soft Rock 2.m3u"
    Update "F:\Documents\Playlists Source\Soul 2007.m3u"
    Update "F:\Documents\Playlists Source\Southbound.m3u"
    Update "F:\Documents\Playlists Source\Speed 50s.m3u"
    Update "F:\Documents\Playlists Source\Steely Dan.m3u"
    Update "F:\Documents\Playlists Source\Traffic.m3u"
    Update "F:\Documents\Playlists Source\Ultimate Who.m3u"
    Update "F:\Documents\Playlists Source\VanMo.m3u"
    Update "F:\Documents\Playlists Source\Walsh.m3u"
    Update "F:\Documents\Playlists Source\Wonder.m3u"

    Sub Update(filename)

    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(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

    End Sub

      My System SpecsSystem Spec

  3.    30 Oct 2008 #13
    Todd Vargo Guest

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


    FenderAxe wrote:

    > Todd Vargo wrote:

    > > For further help, please copy/paste the exact code that returns the
    > > "Disk not ready" message so we can examine it.
    > >
    >
    > I didn't try it using example specs, I replaced the values with the actual
    > names of my files. I was just indicating that I had added the "Update
    > 'filename'" section and that because of this the script no longer works.
    >
    > Here is the code I've run and received the disk not ready error:
    You did not indicate which line reports the error therefore I have to guess
    below.


    >
    > Update "F:\Documents\Playlists Source\1968 Top 100.m3u"
    > Update "F:\Documents\Playlists Source\1972 Top 100.m3u"
    > Update "F:\Documents\Playlists Source\1973 Top 100.m3u"
    ....

    >
    > Sub Update(filename)
    >
    > Const ForReading = 1
    > Const ForWriting = 2
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
    > ForReading)
    Line above is hard coded to drive I:. It should be the variable named
    'filename' as originally posted. Since drive F: is not being accessed, it
    must be drive I: that was not ready either here or below. In effect, you are
    bypassing the long list of files which you intended to have updated above.


    >
    > strText = objFile.ReadAll
    > objFile.Close
    >
    > strNewText = Replace(strText, "J:\Music\Blues", "H:\Media2\Blues")
    ....

    >
    > Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
    > ForWriting)
    Same issue with drive I: here. What type of drive is I:? It must be able to
    be written to (on the fly) otherwise that could be what is giving the drive
    not ready message even with media in the drive.



    > objFile.WriteLine strNewText
    > objFile.Close
    >
    > End Sub

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

      My System SpecsSystem Spec

  4.    31 Oct 2008 #14
    Todd Vargo Guest

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


    Todd Vargo wrote:

    > FenderAxe wrote:

    > > Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and Faves.m3u",
    > > ForWriting)
    >
    > Same issue with drive I: here. What type of drive is I:? It must be able
    to

    > be written to (on the fly) otherwise that could be what is giving the
    drive

    > not ready message even with media in the drive.
    I just checked it with a CD in the drive. I get "permission denied".

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

      My System SpecsSystem Spec

  5.    31 Oct 2008 #15
    FenderAxe Guest

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


    FenderAxe <fa@xxxxxx> wrote in
    news:Xns9B47F3B332CD2faaxecom@xxxxxx:

    > "Todd Vargo" <tlvargo@xxxxxx> wrote in
    > news:OKCze5wOJHA.3748@xxxxxx:
    >

    >> Todd Vargo wrote:

    >>> FenderAxe wrote:
    >>> > Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and
    >>> > Faves.m3u", ForWriting)
    >>>
    >>> Same issue with drive I: here. What type of drive is I:? It must be
    >>> able
    >> to

    >>> be written to (on the fly) otherwise that could be what is giving
    >>> the
    >> drive

    >>> not ready message even with media in the drive.
    >>
    >> I just checked it with a CD in the drive. I get "permission denied".
    >>
    >
    > Hi Todd --
    >
    > Thanks for all of your comments. The reason drive I is mentioned is
    > because I was testing the short version of the script on a computer
    > other than the one it is eventually going to run on, but for this last
    > test I moved the script to the correct computer, but didn't change
    > that part of the script to use the variable "filename." The computer
    > the script is running on doesn't have a drive I, which explains the
    > error.
    >
    > I will modify those two lines as follows:
    >
    > Set objFile = objFSO.OpenTextFile(filename, ForReading)
    >
    > Set objFile = objFSO.OpenTextFile(filename, ForWriting)
    >
    > And try running the script again.
    >
    > Thanks again for your assistance with this.
    >
    > FA
    >
    OK, I just ran a test of the script a couple of times, once by typing the
    execution command at a prompt and one time by having the execution command
    run from a batch file -- and it worked great both times.

    I'm just thrilled, and I can't tell you how much I appreciate your help.
    This is going to save me hundreds of hours. Thank you so much.

    Now I'm going to be on the lookout for more things I can write scripts for.
    <g>

    Also, just so you know, while I can't assist anyone (yet) with scripting, I
    have areas of expertise and I assist people in newsgroups on a weekly, and
    sometimes daily, basis.

    Thanks again everyone --

    FA
      My System SpecsSystem Spec

  6.    01 Nov 2008 #16
    Al Dunbar Guest

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



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

    > FenderAxe <fa@xxxxxx> wrote in
    > news:Xns9B47F3B332CD2faaxecom@xxxxxx:
    >

    >> "Todd Vargo" <tlvargo@xxxxxx> wrote in
    >> news:OKCze5wOJHA.3748@xxxxxx:
    >>

    >>> Todd Vargo wrote:
    >>>> FenderAxe wrote:
    >>>> > Set objFile = objFSO.OpenTextFile("I:\Documents\WSH\Bs and
    >>>> > Faves.m3u", ForWriting)
    >>>>
    >>>> Same issue with drive I: here. What type of drive is I:? It must be
    >>>> able
    >>> to
    >>>> be written to (on the fly) otherwise that could be what is giving
    >>>> the
    >>> drive
    >>>> not ready message even with media in the drive.
    >>>
    >>> I just checked it with a CD in the drive. I get "permission denied".
    >>>
    >>
    >> Hi Todd --
    >>
    >> Thanks for all of your comments. The reason drive I is mentioned is
    >> because I was testing the short version of the script on a computer
    >> other than the one it is eventually going to run on, but for this last
    >> test I moved the script to the correct computer, but didn't change
    >> that part of the script to use the variable "filename." The computer
    >> the script is running on doesn't have a drive I, which explains the
    >> error.
    >>
    >> I will modify those two lines as follows:
    >>
    >> Set objFile = objFSO.OpenTextFile(filename, ForReading)
    >>
    >> Set objFile = objFSO.OpenTextFile(filename, ForWriting)
    >>
    >> And try running the script again.
    >>
    >> Thanks again for your assistance with this.
    >>
    >> FA
    >>
    >
    > OK, I just ran a test of the script a couple of times, once by typing the
    > execution command at a prompt and one time by having the execution command
    > run from a batch file -- and it worked great both times.
    >
    > I'm just thrilled, and I can't tell you how much I appreciate your help.
    > This is going to save me hundreds of hours. Thank you so much.
    >
    > Now I'm going to be on the lookout for more things I can write scripts
    > for.
    > <g>
    LOL. You can write scripts for anything, so I'd stick with the ones that
    will give the best return. Maybe you will be able to save a few hundred more
    hours...

    > Also, just so you know, while I can't assist anyone (yet) with scripting,
    > I
    > have areas of expertise and I assist people in newsgroups on a weekly, and
    > sometimes daily, basis.
    That's what it's all about. And it probably won't be long before you will be
    assisting with scripting. I felt the same way when I started.

    /Al


      My System SpecsSystem Spec

Page 2 of 2 FirstFirst 12
Newbie question: replace multiple strings in multiple text files

Similar Threads
Thread Forum
Multiple File Search and Replace
I would be grateful for any help anyone can give with this VBS script. I originally just wanted to have a script which would cycle through a folder...
General Discussion
RE: Reoplace text in multiple word Docs
Hi I found this guys examples excellent. Some samples may have slight typo's but his work thought me so much. ...
PowerShell
Replace text in multiple text files
Im trying to Replace text in multiple text files using the below mentioned code...but im not able to do it. Ex: Text File Content Line 1 Line 2...
VB Script
How to test if string contains one of multiple strings
How can I test if a string contains one of several possible strings. Whereas I can use the contains or match operator like so: if ($string -match...
PowerShell
Remove lines from multiple text files
Hi, Bit of a noob question - I came across this bit of code the other day: $f=${C:\Somepath\file.txt} $f=$null ${C:\Somepath\file.txt}=$f ...
PowerShell
find and replace a specific string in multiple files
basically, i have a vbscript which does a find and replace on single file, below i've shared the script code. ' replace.vbs Find Replacewith File...
VB Script
replacing text on multiple text files
Hello. I am looking for a way to automatically replace text on multiple text files. I am writing my first script for work, but I've hit a brick...
VB Script

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 23:44.
    .