Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something Fancy - ASP Page to pull up htm files and titles 2

Status
Not open for further replies.

ppedersen1

Technical User
Oct 14, 2002
43
US
Hello, I am trying to do something fancy! I would like to create a page looks in a certain directory for .htm files and pulls out the title in the document and then creates a link for the doc. This would make it easy to link to a bunch of documents without much coding. And if you put a new .htm file in the folder, it would automatically get added to the page when you reopen it. Is this possible? Does the FileSystemObject do something like this? Can somebody help?
 
Hello,
This is possible yes. At least when the folders you want to display are on the same server as the one your asp page is on.
The FileSystemObject is indeed what you need.
See the following url for examples on working with the filesystemobject:

For your needs you only need to open a folder and loop through the contents of it... adding <a> tags around every item to make links. To get the title from a html file I would open the file as a textstream and use a regular expression to filter out what's between the title tags, but I'm sure someone will give you an easier solution for that...

greetings
 
Here's an example I got from goheat that I've modified slightly. Might save you some time.

Code:
<%
' Change the DIRECTORY to point to any virtual directory of your choice.
CONST DIRECTORY = &quot;/&quot; ' relative path in virtual directories

' Specify one of these constants for &quot;sortBy&quot;...
CONST FILE_NAME = 0
CONST FILE_EXT = 1
CONST FILE_TYPE = 2
CONST FILE_SIZE = 3
CONST FILE_CREATED = 4
CONST FILE_MODIFIED = 5
CONST FILE_ACCESSED = 6

' get requested sort order, if not first time here...
' (forward by name is default)
req = Request(&quot;sortBy&quot;)
If Len(req) < 1 Then sortBy = 0 Else sortBy = CInt(req)
req = Request(&quot;priorSort&quot;)
If Len(req) < 1 Then priorSort = -1 Else priorSort = CInt(req)

'
' did user ask for same sort? to reverse the order?
' but if so, then zap priorSort so clicking again will do forward!
If sortBy = priorSort Then
    reverse = true
    priorSort = -1
Else
    reverse = false
    priorSort = sortBy
End If

' now start the *real* code...
'
path = Server.MapPath( DIRECTORY )

Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set theCurrentFolder = fso.GetFolder( path )
Set curFiles = theCurrentFolder.Files
'
' And now a loop for the files
'
Dim theFiles( )
ReDim theFiles( 500 ) ' arbitrary size!
currentSlot = -1 ' start before first slot

' We collect all the info about each file and put it into one
' &quot;slot&quot; in our &quot;theFiles&quot; array.
'
For Each fileItem in curFiles
    fname = fileItem.Name
    fext = InStrRev( fname, &quot;.&quot; )
    If fext < 1 Then fext = &quot;&quot; Else fext = Mid(fname,fext+1)
    ftype = fileItem.Type
    fsize = fileItem.Size
    fcreate = fileItem.DateCreated
    fmod = fileItem.DateLastModified
    faccess = fileItem.DateLastAccessed
    currentSlot = currentSlot + 1
    If currentSlot > UBound( theFiles ) Then
        ReDim Preserve theFiles( currentSlot + 99 )
    End If
    ' note that what we put here is an array!
    theFiles(currentSlot) = Array(fname,fext,ftype,fsize,fcreate,fmod,faccess)
Next
'
' files are now in the array...
'
' As noted, it is actually an ARRAY *OF* ARRAYS. Which makes
' picking the column we will sort on easier!
'
' ...size and sort it...
fileCount = currentSlot ' actually, count is 1 more, since we start at 0
ReDim Preserve theFiles( currentSlot ) ' really not necessary...just neater!

' First, determine which &quot;kind&quot; of sort we are doing.
' (VarType=8 means &quot;string&quot;)
'
If VarType( theFiles( 0 )( sortBy ) ) = 8 Then
    If reverse Then kind = 1 Else kind = 2 ' sorting strings...
Else
    If reverse Then kind = 3 Else kind = 4 ' non-strings (numbers, dates)
End If

'
' A simple bubble sort for now...easier to follow the code...
'
For i = fileCount TO 0 Step -1
    minmax = theFiles( 0 )( sortBy )
    minmaxSlot = 0
    For j = 1 To i
        Select Case kind ' which kind of sort are we doing?
        ' after the &quot;is bigger/smaller&quot; test (as appropriate),
        ' mark will be true if we need to &quot;remember&quot; this slot...
        Case 1 ' string, reverse...we do case INsensitive!
            mark = (strComp( theFiles(j)(sortBy), minmax, vbTextCompare ) < 0)
        Case 2 ' string, forward...we do case INsensitive!
            mark = (strComp( theFiles(j)(sortBy), minmax, vbTextCompare ) > 0)
        Case 3 ' non-string, reverse ...
            mark = (theFiles( j )( sortBy ) < minmax)
        Case 4 ' non-string, forward ...
            mark = (theFiles( j )( sortBy ) > minmax)
        End Select
        ' so is the current slot bigger/smaller than the remembered one?
        If mark Then
            ' yep, so remember this one instead!
            minmax = theFiles( j )( sortBy )
            minmaxSlot = j
        End If
    Next
    ' is the last slot the min (or max), as it should be?
    If minmaxSlot <> i Then
        ' nope...so do the needed swap...
        temp = theFiles( minmaxSlot )
        theFiles( minmaxSlot ) = theFiles( i )
        theFiles( i ) = temp
    End If
Next
' Ta-da! The array is sorted!
'
%>
<FORM Name=&quot;doSort&quot; Method=&quot;Get&quot;>
<INPUT Type=Hidden Name=priorSort Value=&quot;<% = priorSort %>&quot;>
<INPUT Type=Hidden Name=sortBy Value=&quot;-1&quot;>
</FORM>

<SCRIPT Language=&quot;JavaScript&quot;>
function reSort( which )
{
    document.doSort.sortBy.value = which;
    document.doSort.submit( );
}
</SCRIPT>

<CENTER>
<FONT Size=&quot;+2&quot;>
Showing <% = (fileCount+1) %> files from directory <% = path %>
</FONT>
<P>
Click on a column heading to sort by that column. Click the same column
again to reverse the sort.
<P>
<TABLE Border=1 CellPadding=3>
<TR>
    <TH><A HREF=&quot;javascript:reSort(0);&quot;>File name</A></TH>
    <TH><A HREF=&quot;javascript:reSort(1);&quot;>Extension</A></TH>
    <TH><A HREF=&quot;javascript:reSort(2);&quot;>Type</A></TH>
    <TH><A HREF=&quot;javascript:reSort(3);&quot;>Size</A></TH>
    <TH><A HREF=&quot;javascript:reSort(4);&quot;>Created</A></TH>
    <TH><A HREF=&quot;javascript:reSort(5);&quot;>Last modified</A></TH>
    <TH><A HREF=&quot;javascript:reSort(6);&quot;>Last accessed</A></TH>
</TR>
<%
' With the array nicely sorted, this part is a piece of cake!
For i = 0 To fileCount
      Response.Write &quot;<TR>&quot; & vbNewLine
    For j = 0 To UBound( theFiles(i) )
      Response.Write &quot;    <TD>&quot;
      if j = 0 then
        Response.Write &quot;<A HREF='&quot; & theFiles(i)(j) & &quot;' TARGET=new>&quot;
      end if
      Response.Write theFiles(i)(j)
      if j = 0 then
        Response.Write &quot;</A>&quot;
      end if
      Response.Write &quot;</TD>&quot; & vbNewLine
    Next
    Response.Write &quot;</TR>&quot; & vbNewLine
Next
%>
</TABLE>


&quot;<a href=&quot;&quot;&quot;&objFile.Name&&quot;&quot;&quot;>&quot;
</BODY>
</HTML>
Hope this helps,
Jessica
[ponytails2]
 
Dear Jessica,
Thanks, and very impressive! A beautiful collection of useful features.

Several more questions though.
1) Is there a way to select strictly &quot;.htm&quot; files?

2) I have determined that your coding works for the web directory that the file is in. Is there a way to pull the data from a virtual network path, i.e., a folder that is my network, but not part of the &quot;web&quot; directories?

3) TomasCrossRoads above mentioned the use of fso and textstream - do you have any coding that would be able to pull out the <title> data from the web page?

Again, I have to thank you for your wonderful coding. It will be very useful. Thanks!
 
#1

For Each fileItem in curFiles
fname = fileItem.Name
fext = InStrRev( fname, &quot;.&quot; )
If fext < 1 Then fext = &quot;&quot; Else fext = Mid(fname,fext+1)
ftype = fileItem.Type
fsize = fileItem.Size
fcreate = fileItem.DateCreated
fmod = fileItem.DateLastModified
faccess = fileItem.DateLastAccessed
currentSlot = currentSlot + 1
If currentSlot > UBound( theFiles ) Then
ReDim Preserve theFiles( currentSlot + 99 )
End If
' note that what we put here is an array!
theFiles(currentSlot) = Array(fname,fext,ftype,fsize,fcreate,fmod,faccess)
Next

in this coding the variable 'fext' contains the file extension, so only thing you need to do is filter out the .htm and .html files
for example:

For Each fileItem in curFiles
fname = fileItem.Name
fext = InStrRev( fname, &quot;.&quot; )
If fext < 1 Then fext = &quot;&quot; Else fext = Mid(fname,fext+1)

If LCase(CStr(fext)) = LCase(CStr(&quot;htm&quot;)) Or LCase(CStr(fext)) = LCase(CStr(&quot;html&quot;)) Then

ftype = fileItem.Type
fsize = fileItem.Size
fcreate = fileItem.DateCreated
fmod = fileItem.DateLastModified
faccess = fileItem.DateLastAccessed
currentSlot = currentSlot + 1
If currentSlot > UBound( theFiles ) Then
ReDim Preserve theFiles( currentSlot + 99 )
End If
' note that what we put here is an array!
theFiles(currentSlot) = Array(fname,fext,ftype,fsize,fcreate,fmod,faccess)

End If 'for file extension check

Next

#3 check out
you could write a function that opens the file as described in the above url and then search it by using a regular expression for the stuff between <title> tags. I guess it would be easiest if you place the title in an extra value in the array.

theFiles(currentSlot) = Array(fname,fext,ftype,fsize,fcreate,fmod,faccess, GiveMeTitle(fname))

instead of:

theFiles(currentSlot) = Array(fname,fext,ftype,fsize,fcreate,fmod,faccess)

Function GiveMeTitle(sFileName)

'coding here for opening the file as textstream !

coding for the regular expression:

'let's assume sTextStream is the textstream
Dim oRegExp, oMatch, sMatch, sTitle
Set oRegExp = new regexp
oRegExp.IgnoreCase = True
oRegExp.Global = True
'this is the regular expression:
oRegExp.Pattern = &quot;(<title>)(\s*\n|.+?\s*)(</title>)&quot;
'test it against the stream:
Set oMatch = oRegExp.Execute( sTextStream )
For Each sMatch IN oMatch
'there will only be one match usually (or none)
'we need the second subpart of the regular expression (what's between the tags)
sTitle = sMatch.SubMatches(1)
Next
Set oRegExp = NOTHING
Set oMatch = NOTHING

If Len(sTitle) > 0 Then
'we found a title
End If

GiveMeTitle = sTitle

End Function

I didn't test it, hope it contains no errors
and i think this code will be slow for large folders (with many files)...

greetings
 
Thanks, ppedersen1! But I have to say goheat wrote it (I couldn't find the original post), I just added the anchor tags bit. :)

Thanks, TomasCrossroads, for answering those questions. I wasn't sure of the answers so I've learned a couple more things today!

Hope this helps,
Jessica
[ponytails2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top