Forum Moderators: phranque

Message Too Old, No Replies

Generating the title's for alot of HTML pages

How to do it?

         

PFOnline

1:07 am on May 23, 2004 (gmt 0)

10+ Year Member



I'm working with alot of files, and basically just need to know how to take the file name and add it to the title.

The files are like this for example: led_zeppelin-kashmir.html

and I would need it to automatically take the file name and add it to the title so the end result would look like this:

MySitesName.com - Led Zeppelin - Kashmir

Anyone know how to do this?

Another example just in case, would be like this...

Filename: rolling_stones-paint_it_black.html

End result for title of this html file: MySitesName.com - Rolling Stones - Paint it Black

PFOnline

10:30 pm on May 23, 2004 (gmt 0)

10+ Year Member



Can anyone help? There has to be a way to do this...

Alternative Future

10:33 pm on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi PFOnline,

This would have to be done server-side, i.e. with the use of JSP, ASP or another.
It might also be possible to do it with CGI (but am not familiar with CGI to either say yes or no)
Do you have access to any of these?

-George

Alternative Future

10:35 pm on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry just checked that JavaScript can also do this!

So corrections to my last post it can be done client-side also :()

Here [javascript.internet.com] is an example.

-George

PFOnline

10:50 pm on May 23, 2004 (gmt 0)

10+ Year Member



Hi A.F.,

I just checked and found I do have access to CGI, but I'm not sure about JSP or ASP, I'll have to find out, and thanks for the link for dynamic title's, I think that's exactly what I'm looking for!

<added>Wait, it appears this script is for making the title change every 10 seconds, and not for how to generate a title for each page from the file name.</added>

ogletree

10:57 pm on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Perl could do it or any language that handles text files even excel. You just need a little program to open each file and insert the tag. When the file is opened you can have that file name stored in a variable. Then just a basic text search and replace.

PFOnline

11:18 pm on May 23, 2004 (gmt 0)

10+ Year Member



Hi ogletree,

Do you think there programs allready out there that can do this, or were you thinking I would have to write a program to do this?

Cause I see what you're saying, but I don't quite understand, and am a little confused on how to do that.

And if I could just get past adding the different titles to each page, I could finally get my site up and running! I've paid for 2 months hosting allready, and my site is still "coming soon" lol.

PFOnline

11:24 pm on May 23, 2004 (gmt 0)

10+ Year Member



I just had an idea... What if I add:

<title>MySitesName.com - #var1# - #var2#</title>

To each page.

But then how would I replace those variables with the file name?

So like if you had a file rolling_stones-paint_it_black.html

The title for this page would be MySitesName.com - Rolling Stones - Paint It Black

Somehow the first part of the file name (before the "-") would have to be assigned to #var1# and the second part (after the "-") assigned to #var2#

Bah, I'm clueless.

Easy_Coder

11:40 pm on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've written an object in VB that does something similar to this. I had to traverse an entire site of over 1000 pages and yank out all of the code from the HTML Tag to the BODY Tag and replace it with an include template file; and remove the closing html and body tag and replace those with another include template. I used the File System Object and some good ole string parsing.

Locating the title start and end tags shouldn't be too difficult. Once you have those positions you can easily write in the file name...

PFOnline

11:56 pm on May 23, 2004 (gmt 0)

10+ Year Member



Yes, but how would you do that exactly? lol, I'm new to making a webpage like this, with so many pages.

This is my 2nd website, and will have about 20,000 individual HTML pages, my first only had about 15, so I was able to write the title tag's by hand.

PFOnline

1:52 am on May 24, 2004 (gmt 0)

10+ Year Member



Would this work?

Maybe something like this, using SSI:

<HTML>
<HEAD>
<title><!--#echo var="title"--></title>
</HEAD>
<BODY>

calling the following in each page.....

<!--#set var="title" value="blahblahblah"-->

Am I on the right track?

Easy_Coder

3:24 am on May 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your gonna need to put together an application that traverses the entire web site and writes in the new titles as the application is looping through the file collection. I'll dig the code out and post it. Are you familier with VB?

PFOnline

3:45 am on May 24, 2004 (gmt 0)

10+ Year Member



Thanks EasyCoder, and I'm just slightly familiar with VB, took a class on it in college, so I guess I kinda know "the basics", but I also don't have VB on this computer.

PFOnline

7:11 pm on May 24, 2004 (gmt 0)

10+ Year Member



Did you happen to find the code EasyCoder?

Or does anyone else have an idea of how I might do this?

Easy_Coder

7:52 pm on May 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah... your gonna have to modify this to VBScript(.vbs file or ASP) if you don't have VB; and also to handle your needs but here's what I had. Just a bit of advice, make sure you back up your stuff before turning your finished script loose. I forgot once and then I had to write another routine to undo everything.

Post back if your not sure how to isolate the title tag value.

I had a form with a text box & button that kicked everything off:
[code]
Private Sub Command1_Click()

Text2.Text = ""

If IsNull(Text1.Text) = True Or Trim(Text1.Text) <> "" Or Right(Trim(Text1.Text), 1) <> "\" Then

'Get & Set the Folder Path Property
FolderPath = Text1.Text

'Kick the Process Off
Call WriteLineToFile(FolderPath)
Else
MsgBox "Enter the File Path"
End If

End Sub

Function WriteLineToFile(filespec)

Const ForReading = 1, ForWriting = 2, conType = "htm"
Dim fso, f, sFileBuffer, fc, x, sExtType
Dim j As Long

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFolder(FolderPath)
Set fc = f.Files
j = 0

Dim sNewFileText As String
Dim sUIFind As String
Dim sUIReplace As String

sUIFind = "Release Notes Request</a>"

sUIReplace = "<br><br><br><br><!--#INCLUDE FILE=""../../include/Footer.asp""-->"


Text2.Visible = True

For Each x In fc

sExtType = fso.GetExtensionName(FolderPath & x.Name)


'File must be HTM extension only
If InStr(1, sExtType, conType) Then

'Get & Read The File into a buffer
Set f = fso.OpenTextFile(filespec + x.Name, ForReading)
sFileBuffer = f.ReadAll


'Check the buffer for our Template
If InStr(1, UCase(sFileBuffer), "<!--#INCLUDE") > 0 Then

'Parsing this file...
Text2.Text = Text2.Text & "Parsing: " + x.Name + vbCrLf

'Update the Template
'Replace the old PageWrap with the new wrapper
sNewFileText = Replace(sFileBuffer, "<!--#INCLUDE VIRTUAL=""/INCLUDE/PageWrap.asp""-->", "<!--#INCLUDE FILE=""../../include/Wrap.asp""-->")

'Replace the old PageWrapUp with the new wrapper
sNewFileText = Replace(sNewFileText, "<!--#INCLUDE VIRTUAL=""/INCLUDE/PageWrapUp.asp""-->", "<!--#INCLUDE FILE=""../../include/WrapUp.asp""-->")

'Update the Page Title Variable
sNewFileText = Replace(sNewFileText, "sTitle", "sPageTitle")

'Remove Old Image Header
If InStr(1, sNewFileText, sUIFind) > 0 Then
sNewFileText = Replace(sNewFileText, sUIFind, sUIReplace)
Else
Text2.Text = Text2.Text & "No Match"
End If




'File is determined to not have a Template, so open up the
'file and wrap our template around the existing contents
Set f = fso.OpenTextFile(filespec + x.Name, ForWriting, True)

'f.WriteLine "<!--#INCLUDE VIRTUAL=""/INCLUDE/Wrap.asp""-->" & sFileBuffer & "<!--#INCLUDE VIRTUAL=""/INCLUDE/WrapUp.asp""-->"
f.WriteLine sNewFileText
f.Close

sNewFileText = ""
sFileBuffer = ""

j = j + 1

Else

'Not parsing this file
Text2.Text = Text2.Text & "Not Parsing: " + x.Name + vbCrLf

End If

End If

Next x

MsgBox "Process Complete: " & j & " files parsed."

End Function

Function ReadEntireFile(filespec)

Const ForReading = 1
Dim fso, theFile, retstring

Set fso = CreateObject("Scripting.FileSystemObject")

Set theFile = fso.OpenTextFile(filespec, ForReading, False)

Do While theFile.AtEndOfStream <> True
retstring = retstring & theFile.ReadLine
Loop

theFile.Close

ReadEntireFile = retstring

End Function

Private mFolderPath As String

Property Get FolderPath() As String
FolderPath = mFolderPath
End Property

Property Let FolderPath(ByVal folderChars As String)
mFolderPath = folderChars
End Property

PFOnline

8:27 pm on May 24, 2004 (gmt 0)

10+ Year Member



Hey EasyCoder, thanks very much for the code, but I guess I don't have the knowledge enough to do it on my own.

I tried saving that code into a .vbs file, but when i tried to open it, it didn't work.

But still thanks again... really appreciate it.

PFOnline

8:48 pm on May 24, 2004 (gmt 0)

10+ Year Member



I just had an idea!

If there was some way to get the file name INTO the actual file itself, i could then do a search and replace for all, adding the title tags next to the file name!

So i guess my question now is: Is there a way to get the file name into the file?

Anyone know!

StupidScript

9:48 pm on May 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PFOnline, you need to do SOME programming for this, unless you are willing to hand-edit everything. Likewise, this programming MUST be able to determine the file name, parse it per your requirements into variable substrings, and then write the TITLE of each page, either as a static TITLE (as if it were hand-coded) or as a dynamic TITLE generated as the page is being delivered or when it is received.

Depending on the cleanliness of your URIs, you should be able to use Javascript for the client-side TITLE generation (not very useful to spiders, though) or something like PHP for server-side generation.

If the page does not have a TITLE until a script-capabale browser opens it, then you will not be gaining any search engine position benefits normally provided by that piece of info. If you CAN do a server-side or static solution, it's preferred.

Doing it with PHP (use includes to put this in every page conveniently):

<?
print("<title>");
# Initialize empty strings
$thisband="";
$thisalbum="";
# Get file name
$thispg=$_SERVER['PHP_SELF'];
# Split into band and album at the hyphen
$thispg_array=explode("-",$thispg);
$first_part=$thispg_array[0];
$second_part=$thispg_array[1];
# Get rid of the slash that is PHP_SELF's first char
$band=substr($first_part,1);
# Get rid of the file extension
$album=explode(".",$second_part);
# Split band into words at the underscore
$band_array=explode("_",$band);
# Split album into words at the underscore
$album_array=explode("_",$album[0]);
# Loop through words, capitalizing them
for($i=0;$i<sizeof($band_array);$i++) {
$thisband.=ucfirst($band_array[$i]);
$thisband.=" ";
}
for($i=0;$i<sizeof($album_array);$i++) {
$thisalbum.=ucfirst($album_array[$i]);
$thisalbum.=" ";
}
# Get rid of the last whitespace char
$thisalbum=rtrim($thisalbum);
# Display the result, adding space-hyphen-space between
echo $thisband." - ".$thisalbum;
print("</title>\n");
?>

(Do a search and replace through all pages in your site and change <title>.*</title> to <?include "get_title.php"?> where the above PHP code is contained in a file named "get_title.php".)

Variations in PHP installations may cause some slight tweaks to the PHP syntax...but not much.

PFOnline

10:10 pm on May 24, 2004 (gmt 0)

10+ Year Member



Hi StupidScripts, I just seconds ago figured out how to do it!

So, sorry you went through the trouble of writing that last post, and thanks though!

If curious, here is the code I used:

<?
// Set main title
$mainTitle = "MySitesName.com";

// Get the name of the current file, minus its extension
$curFile = basename(__FILE__, ".php");

// Replace all the "_" with spaces
$curFile = str_replace("_", " ", $curFile);

// Split it up to Artist and Song by the "-"
list($artist,$song) = explode("-",$curFile);

$fullTitle = $mainTitle . " - " . $artist . " - " . $song;
?>
<html>
<head>
<title><?= $fullTitle?></title>
</head>

StupidScript

4:55 pm on May 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice and neat. Good!