page title in a list of recently updated pages

Discussion in 'ASP General' started by nospam@plasticlegs.com, Apr 19, 2006.

  1. Guest

    Hi there,

    I'm using VBScript to display a list of the ten most recently updated
    pages on my web site. Right now, the script lists the filenames and
    the date modified in a given directory.

    What I want to know is if there is any way to extract the page title
    and display that instead of the file name? Can I use .asp and VBscript
    to "delve" into the file and extract the title?

    Here's the code I'm using:
    <%
    folder = ".\"
    set fso = CreateObject("Scripting.fileSystemObject")
    set fold = fso.getFolder(Server.MapPath(folder))
    fileCount = fold.files.count
    dim fNames(), fDates()
    redim fNames(fileCount), fDates(fileCount)
    cFcount = 0
    for each file in fold.files
    cFcount = cFcount + 1
    fNames(cFcount) = lcase(file.name)
    fDates(cFcount) = file.dateLastModified
    next
    for tName = 1 to fileCount
    for nName = (tName + 1) to fileCount
    if (fDates(tName) < fDates(nName)) then
    buffer = fNames(nName)
    dateBuffer = fDates(nName)
    fNames(nName) = fNames(tName)
    fDates(nName) = fDates(tName)
    fNames(tName) = buffer
    fDates(tName) = dateBuffer
    end if
    next
    next
    if (fileCount > 10) then
    fileCount = 10
    End If
    Response.Write "<table border=1 width='90%'>"
    for i = 1 to fileCount
    Response.Write "<tr><td><a href='" & fNames(i) & "'>" &
    fNames(i) & "</a></td><td>" & fDates(i) & "</td></tr>"
    next
    Response.Write "</table>"
    %>
     
    , Apr 19, 2006
    #1
    1. Advertising

  2. Mike Brind Guest

    wrote:
    > Hi there,
    >
    > I'm using VBScript to display a list of the ten most recently updated
    > pages on my web site. Right now, the script lists the filenames and
    > the date modified in a given directory.
    >
    > What I want to know is if there is any way to extract the page title
    > and display that instead of the file name? Can I use .asp and VBscript
    > to "delve" into the file and extract the title?
    >
    > Here's the code I'm using:
    > <%
    > folder = ".\"
    > set fso = CreateObject("Scripting.fileSystemObject")
    > set fold = fso.getFolder(Server.MapPath(folder))
    > fileCount = fold.files.count
    > dim fNames(), fDates()
    > redim fNames(fileCount), fDates(fileCount)
    > cFcount = 0
    > for each file in fold.files
    > cFcount = cFcount + 1
    > fNames(cFcount) = lcase(file.name)
    > fDates(cFcount) = file.dateLastModified
    > next
    > for tName = 1 to fileCount
    > for nName = (tName + 1) to fileCount
    > if (fDates(tName) < fDates(nName)) then
    > buffer = fNames(nName)
    > dateBuffer = fDates(nName)
    > fNames(nName) = fNames(tName)
    > fDates(nName) = fDates(tName)
    > fNames(tName) = buffer
    > fDates(tName) = dateBuffer
    > end if
    > next
    > next
    > if (fileCount > 10) then
    > fileCount = 10
    > End If
    > Response.Write "<table border=1 width='90%'>"
    > for i = 1 to fileCount
    > Response.Write "<tr><td><a href='" & fNames(i) & "'>" &
    > fNames(i) & "</a></td><td>" & fDates(i) & "</td></tr>"
    > next
    > Response.Write "</table>"
    > %>


    Yes. You can use the Scripting.FileSystemObject to read the contents
    of an asp file, and then a regular expression to get the title. Here's
    one that opens a file in the same folder and finds the title. It
    assumes that you will only have letters, numbers or spaces in the
    title:

    <%
    Dim objFSO, objTextStream, strSearchOn, objMatch, colmatches, mymatch
    Dim strFileName
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    strFileName = Server.Mappath("filename.asp")
    const fsoForReading = 1
    Set objTextStream = objFSO.OpenTextFile(strFileName, fsoForReading)
    strSearchOn = objTextStream.ReadAll
    Set objRegExpr = New regexp
    objRegExpr.Pattern = "<title>[\w\d\s]*<"
    objRegExpr.Global = True
    objRegExpr.IgnoreCase = True
    set colmatches = objRegExpr.Execute(strSearchOn)
    For Each objMatch in colMatches
    mymatch = replace(objMatch.Value,"<title>","")
    mymatch = replace(mymatch,"<","")
    Next
    Response.Write mymatch
    objTextStream.Close
    Set objTextStream = Nothing
    Set objFSO = Nothing
    %>

    --
    Mike Brind
     
    Mike Brind, Apr 19, 2006
    #2
    1. Advertising

  3. Guest

    Thanks Mike! I was able to insert that script into my code to give me
    exactly what I want! One last thing - is there any way to allow dashes
    in the title?
     
    , Apr 20, 2006
    #3
  4. Mike Brind Guest

    wrote:
    > Thanks Mike! I was able to insert that script into my code to give me
    > exactly what I want! One last thing - is there any way to allow dashes
    > in the title?


    Yes. Add \- to the pattern within the square brackets, so the line
    should read:

    objRegExpr.Pattern = "<title>[\w\d\s\-]*<"

    Hyphens/Dashes need to be escaped with a backslash because it's one of
    the special characters. The pattern above first looks for the text
    <title>, followed by any word character or digit or whitespace or dash
    appearing 0 or more times before an opened angle bracket.

    If you find you need to add more options (double colons :: seem to be
    the rave with some people), have a look at this article:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting051099.asp

    --
    Mike Brind
     
    Mike Brind, Apr 20, 2006
    #4
  5. Evertjan. Guest

    Mike Brind wrote on 20 apr 2006 in microsoft.public.inetserver.asp.general:

    > Yes. Add \- to the pattern within the square brackets, so the line
    > should read:
    >
    > objRegExpr.Pattern = "<title>[\w\d\s\-]*<"
    >


    Why not more general:

    "<title>[^<]*<"

    ?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Apr 20, 2006
    #5
  6. Guest

    Thanks Mike! I was able to insert that script into my code to give me
    exactly what I want! One last thing - is there any way to allow dashes
    in the title?

    Oops, never mind, I just solved it:

    Replace
    objRegExpr.Pattern = "<title>[\w\d\s]*<"

    With
    objRegExpr.Pattern = "<title>[\w\d\s\x2D]*<"

    Thanks again!
     
    , Apr 20, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    1
    Views:
    498
    Steve C. Orr, MCSD
    Aug 10, 2003
  2. Replies:
    0
    Views:
    577
  3. Replies:
    1
    Views:
    595
    David
    Apr 24, 2007
  4. soonic
    Replies:
    11
    Views:
    1,850
    soonic
    Jul 27, 2011
  5. Dmitry Korolyov

    Datagrid not updated during delete, but updated during insert and update

    Dmitry Korolyov, Sep 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    445
    Dmitry Korolyov
    Sep 22, 2003
Loading...

Share This Page