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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding file hyperlinks to filenames in an ASP list page? 1

Status
Not open for further replies.

goheat

Technical User
Apr 12, 2002
5
0
0
US
Hi. I want to create a page that lists filenames from a directory, with the ability to sort by chosen columns. I found the sample code below which will do so, but how would I add the ability to create a hyperlink to the actual files so the viewer could click on the filename in the list and open/download the file? Any help would be most appreciated!

Thanks,

John Suit
NCA

<%
' 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; & theFiles(i)(j) & &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>
 
You'll want to put an <A> tag around the file name pointing to it...

Code:
Response.Write &quot;    <TD><A HREF='&quot; theFilesloc(i) &quot;' TARGET=new>&quot; _ 
& theFiles(i)(j) & &quot;</A></TD>&quot; & vbNewLine

So you'll end up with:
Code:
<TD><A HREF='files/word.doc' TARGET=new>word.doc</A></TD>

Good luck!
 
Thanks, jlsmithprism, but I still have a problem. This is the change I made:

' 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><A HREF='&quot; theFilesloc(i) &quot;' TARGET=new>&quot; _
& theFiles(i)(j) & &quot;</A></TD>&quot; & vbNewLine
Response.Write &quot;</TR>&quot; & vbNewLine
Next
%>

Is that correct? When I did that, I get an IE error when going to the page:

The page cannot be displayed
There is a problem with the page you are trying to reach and it cannot be displayed.

I cut and pasted your changes. Am I still missing anything?

Thanks so much for your assistance!

John
 
Hi!

Okay, I spoke a little too soon w/o testing! :)

Try this...it works for me...
Code:
<%
' 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;
      ' first column is file name
      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
%>


Jessica
 
That worked perfectly! Thank you so much, Jessica. I've been stewing over this problem for a week.

I truly appreciate your knowledge!

John
 
Hi there,

a nice app thus far, just wondering if it's possible to display the file path for the files and to list subdirectories?

cheers
m Mark Saunders :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top