Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Advanced find and replace using VBScript

Reply
 
Old 12-02-2008   #1 (permalink)
Matthew Schwarz


 
 

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
Old 12-02-2008   #2 (permalink)
Todd Vargo


 
 

Re: Advanced find and replace using VBScript

Matthew Schwarz wrote:
Quote:

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

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

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

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

Thread Tools


Similar Threads
Thread Forum
find/replace on file VB Script
Find & Replace in MSSQL Tables through PowerShell PowerShell
Find and Replace Utility ? Vista General
Advanced search - How to find something between dates? Vista General
Find/Replace PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46