Newbie question: replace multiple strings in multiple text files


  1.    26 Oct 2008 #1
    FenderAxe Guest

    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

  2.    26 Oct 2008 #2
    Richard Mueller [MVP] Guest

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


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

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

  3.    26 Oct 2008 #3
    Pegasus \(MVP\) Guest

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



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

  4.    26 Oct 2008 #4
    FenderAxe Guest

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


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

    > 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...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
      My System SpecsSystem Spec

  5.    26 Oct 2008 #5
    Todd Vargo Guest

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



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

  6.    26 Oct 2008 #6
    Richard Mueller [MVP] Guest

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



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

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

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

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


      My System SpecsSystem Spec

  7.    26 Oct 2008 #7
    FenderAxe Guest

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


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

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

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

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

  8.    27 Oct 2008 #8
    FenderAxe Guest

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


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

    >
    > "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...anda/feb05/hey
    > 0208

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

  9.    27 Oct 2008 #9
    Todd Vargo Guest

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


    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.

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

      My System SpecsSystem Spec

  10.    27 Oct 2008 #10
    Todd Vargo Guest

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


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

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

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
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 10:56.
    .