Forum Moderators: open

Message Too Old, No Replies

Directory listing - excluding certain file types

         

Darkelve

8:39 am on Apr 8, 2008 (gmt 0)

10+ Year Member



I found the code below on the internet, it does just what I need. Except, it also includes the html/asp files in the parent directory. I want the script not to show those.

So how can I create some sort of exception, so that it checks for .asp, .html files and does not display those?

Or, alternatively, change the script so that it does not list files (but still lists folders) that are in the parent directory?


<% OPTION EXPLICIT %>
<% sub ListFolderContents(path)
dim fs, folder, file, item, url
set fs = CreateObject("Scripting.FileSystemObject")
set folder = fs.GetFolder(path)

'Display the target folder and info.
Response.Write("<li><strong>" & folder.Name & "</strong> " )


Response.Write("<ul>" & vbCrLf)

'Display a list of sub folders.
for each item in folder.SubFolders
ListFolderContents(item.Path)
next

'Display a list of files.
for each item in folder.Files
url = MapURL(item.path)

Response.Write("<li><a href=""" & url & """>" & item.Name & "</a> </li>" & vbCrLf)

next
Response.Write("</ul>" & vbCrLf)
Response.Write("</li>" & vbCrLf)
end sub

function MapURL(path)
dim rootPath, url
'Convert a physical file path to a URL for hypertext links.
rootPath = Server.MapPath("/")
url = Right(path, Len(path) - Len(rootPath))
MapURL = Replace(url, "\", "/")
end function %>

[edited by: Darkelve at 8:40 am (utc) on April 8, 2008]

Response

5:03 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



Darkelve, try this.
Note the sFileExclusions string and the bFoldersOnly flag
to get the effect you are looking for.

<% OPTION EXPLICIT %>

<% sub ListFolderContents(path)
dim fs, folder, file, item, url
set fs = CreateObject("Scripting.FileSystemObject")
set folder = fs.GetFolder(path)

' Output Only Folders Flag
dim bFoldersOnly
bFoldersOnly = False

' File extension exclusion list
' Currently excluding HTML files
' Comma required to seperate file extensions
dim sFileExclusions
dim sExtension
sFileExclusions = "HTML,HTM,"

'Display the target folder and info.
Response.Write("<li><strong>" & folder.Name & "</strong> " )

Response.Write("<ul>" & vbCrLf)

'Display a list of sub folders.
for each item in folder.SubFolders
ListFolderContents(item.Path)
next

'Display a list of files.
If NOT(bFoldersOnly) Then
for each item in folder.Files
sExtension = UCase(Right(item.name,len(item.name)-InStrRev(item.name, ".")))
If (InStr(sFileExclusions, sExtension)=0) Then
url = MapURL(item.path)
Response.Write("<li><a href=""" & url & """>" & item.Name & "</a> </li>" & vbCrLf)
End If
next
End If
Response.Write("</ul>" & vbCrLf)
Response.Write("</li>" & vbCrLf)
end sub

function MapURL(path)
dim rootPath, url
'Convert a physical file path to a URL for hypertext links.
rootPath = Server.MapPath("/")
url = Right(path, Len(path) - Len(rootPath))
MapURL = Replace(url, "\", "/")
end function %>

Darkelve

6:30 am on Apr 9, 2008 (gmt 0)

10+ Year Member



Hi there,

thanks an awful lot for such a useful and elegant solution!