I\'ve used the script below to grab a list of files that exist in a specific directory, this will also sort the filenames alphabetically. All that you have to change is the CONST DIRECTORY variable to the relative root to the directory that you want to pull file names from.
<%
CONST DIRECTORY = "/UserFiles/documents/" \' relative path in virtual directories
\' Specify one of these constants for "sortBy"...
CONST FILE_NAME = 1
CONST FILE_SIZE = 2
CONST FILE_MODIFIED = 3
\' get requested sort order, if not first time here...
\' (forward by name is default)
req = Request("sortBy")
If Len(req) < 1 Then sortBy = 0 Else sortBy = CInt(req)
req = Request("priorSort")
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("Scripting.FileSystemObject")
Set theCurrentFolder = fso.GetFolder( path )
Set curFiles = theCurrentFolder.Files
\' And now a loop for the files
\'
Dim imageFiles( )
ReDim imageFiles( 500 ) \' arbitrary size!
currentSlot = -1 \' start before first slot
\' We collect all the info about each file and put it into one
\' "slot" in our "theFiles" array.
\'
counter = 0
For Each fileItem in curFiles
counter = counter + 1
fname = fileItem.Name
fsize = fileItem.Size
fmod = fileItem.DateLastModified
\' note that what we put here is an array!
currentSlot = currentSlot + 1
If currentSlot > UBound( imageFiles ) Then
ReDim Preserve imageFiles( currentSlot + 99 )
End If
imageFiles(currentSlot) = Array(fname,fsize,fmod)
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 "kind" of sort we are doing.
\' (VarType=8 means "string")
\'
If counter > 0 Then
If VarType( ImageFiles( 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 = ImageFiles( 0 )( sortBy )
minmaxSlot = 0
For j = 1 To i
Select Case kind \' which kind of sort are we doing?
\' after the "is bigger/smaller" test (as appropriate),
\' mark will be true if we need to "remember" this slot...
Case 1 \' string, reverse...we do case INsensitive!
mark = (strComp( imageFiles(j)(sortBy), minmax, vbTextCompare ) < 0)
Case 2 \' string, forward...we do case INsensitive!
mark = (strComp( imageFiles(j)(sortBy), minmax, vbTextCompare ) > 0)
Case 3 \' non-string, reverse ...
mark = (imageFiles( j )( sortBy ) < minmax)
Case 4 \' non-string, forward ...
mark = (imageFiles( 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 = imageFiles( 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 = imageFiles( minmaxSlot )
imageFiles( minmaxSlot ) = imageFiles( i )
imageFiles( i ) = temp
End If
Next
\' Ta-da! The array is sorted!
\'
End IF
%>
No in order to loop through a list the file names you can use the code below. This code actually displays the filename in a link so that I can click it to open up the file in a browser.
<%
counter = 0
For i = 0 To fileCount
counter = counter + 1
%>
<a href="http://www.yourdomain.com/UserFiles/documents/<%= ImageFiles(i)(0) %>"><%= ImageFiles(i)(0) %></a><br>
<%
Next
If counter = 0 then %>
There are currently no documents to view.
<% end if %>