Forum Moderators: phranque
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
So corrections to my last post it can be done client-side also :()
Here [javascript.internet.com] is an example.
-George
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>
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.
<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.
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...
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
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.
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>