Windows Vista Forums

Advanced find and replace using VBScript
  1. #1


    Matthew Schwarz Guest

    Advanced find and replace using VBScript

    Howdy,

    I posted this in another section and was recommended to make this post in
    the VBScript section.

    Since I can't find any programs to do what I'd like to do, I tried to
    experiment using DOS commands from within WinXP.

    I have 1,262 .htm files. Almost all of them have a headline that is
    surrounded by the exact same strings --
    In the front: <font face="Arial" size="5"><b>
    And in the back: </b></font>

    Some have the tags in backwards order --
    In the front: <b><font face="Arial" size="5">
    And in the back: </font></b>



    Also, every single one of them has near the top of the file this:
    <title>News -- Commander, U.S. 7th Fleet</title>

    I'd like to replace the "News -- Commander, U.S. 7th Fleet" with the
    headline that is in between the tags I meantioned at the beginning of this
    post.

    So I tried a quick test using FIND.

    Something like: find *.htm "<title>" > "</title>" > test.txt

    Voila, everything that began with a <title> and ended with </title> was
    input into test.txt, and each file name was included too.

    I wasn't too worried that test.txt contained not only what was INSIDE the
    <title> tags but the tags themselves, because I can use FrontPage's "find and
    replace" later on to clean that up.

    When I tried something similar using the font tags I mentioned above,
    everything got kind of hairy. I believe it is because strings are supposed to
    be identified inside quotes and the tags contain quotes themselves, so FIND
    got confused.

    Even if it worked I wouldn't know what to do next. I would have a file
    called test.txt that contained all the headlines I wanted, but I wouldn't
    know how to put them inside the <title> tags.

    Can anyone help? I don't know anything about VBScript ...

    Thank you very much.


      My System SpecsSystem Spec

  2. #2


    Todd Vargo Guest

    Re: Advanced find and replace using VBScript

    Matthew Schwarz wrote:

    > Howdy,
    >
    > I posted this in another section and was recommended to make this post in
    > the VBScript section.
    >
    > Since I can't find any programs to do what I'd like to do, I tried to
    > experiment using DOS commands from within WinXP.
    >
    > I have 1,262 .htm files. Almost all of them have a headline that is
    > surrounded by the exact same strings --
    > In the front: <font face="Arial" size="5"><b>
    > And in the back: </b></font>
    >
    > Some have the tags in backwards order --
    > In the front: <b><font face="Arial" size="5">
    > And in the back: </font></b>
    >
    > Also, every single one of them has near the top of the file this:
    > <title>News -- Commander, U.S. 7th Fleet</title>
    >
    > I'd like to replace the "News -- Commander, U.S. 7th Fleet" with the
    > headline that is in between the tags I meantioned at the beginning of this
    > post.
    >
    > So I tried a quick test using FIND.
    >
    > Something like: find *.htm "<title>" > "</title>" > test.txt
    >
    > Voila, everything that began with a <title> and ended with </title> was
    > input into test.txt, and each file name was included too.
    >
    > I wasn't too worried that test.txt contained not only what was INSIDE the
    > <title> tags but the tags themselves, because I can use FrontPage's "find
    and

    > replace" later on to clean that up.
    >
    > When I tried something similar using the font tags I mentioned above,
    > everything got kind of hairy. I believe it is because strings are supposed
    to

    > be identified inside quotes and the tags contain quotes themselves, so
    FIND

    > got confused.
    >
    > Even if it worked I wouldn't know what to do next. I would have a file
    > called test.txt that contained all the headlines I wanted, but I wouldn't
    > know how to put them inside the <title> tags.
    >
    > Can anyone help? I don't know anything about VBScript ...
    >
    > Thank you very much.
    Create a test folder and copy a few of the files needing modified to it.
    Then save this VBScript file in the same folder and double click it.
    Does it do what you want?

    'ReTitle-htms.vbs
    Const ForReading = 1, ForWriting = 2
    Set fso = CreateObject("Scripting.FileSystemObject")
    title = "<title>News -- Commander, U.S. 7th Fleet</title>"
    count = 0
    Set f = fso.GetFolder(".")
    Set fc = f.Files
    For Each f1 in fc
    If Ucase(Right(f1.Name, 4)) = ".HTM" Then
    Set f2 = fso.OpenTextFile (f1.Name, ForReading)
    arr = Split(f2.Read(f1.Size), vbCRLF)
    f2.Close

    For i = 0 to Ubound(arr)
    headline = ""
    If Left(arr(i), 31) = "<font face=""Arial"" size=""5""><b>"_
    And Right(arr(i), 11) = "</b></font>" Then
    headline = Mid(arr(i), 32, Len(arr(i)) - 42)
    Exit For
    ElseIf Left(arr(i), 31) = "<b><font face=""Arial"" size=""5"">"_
    And Right(arr(i), 11) = "</font></b>" Then
    headline = Mid(arr(i), 32, Len(arr(i)) - 42)
    Exit For
    End If
    Next

    If Not headline = "" Then
    For i = 0 to Ubound(arr)
    If arr(i) = title Then
    arr(i) = "<title>" & Trim(headline) & "</title>"
    Set f2 = fso.OpenTextFile (f1.Name, ForWriting, True)
    f2.Write Join(arr, vbCRLF)
    f2.Close
    count = count + 1
    Exit For
    End If
    Next
    End If
    End If
    Next

    Wscript.Echo count & " files have been modified."
    'end of ReTitle-htms.vbs

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


      My System SpecsSystem Spec

Advanced find and replace using VBScript problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
VBScript String Replace - Remove / Replace Characters in String dsoutter VB Script 17 05 Mar 2010
VBScript String Clean function - remove or replace a set ofcharacters dsoutter VB Script 4 30 Dec 2009
VBScript String Clean function - remove or replace illegal characters dsoutter VB Script 2 21 Dec 2009
VBScript: Search/Replace Issues Jen P. VB Script 5 14 Oct 2009
Find/Replace eric.eickhoff@sbcglobal.net PowerShell 5 28 Mar 2007