Walking the Directory Structure
Here is the code that makes this all work. It wasn't until after I wrote this script that I discovered the
GetParentFolder method, which probably would have made life a bit easier, but then,
I'm used to taking the long way around. *big grin* I will eventually get around to rewriting this
script (as soon as I have a moment to spare!), but until I do, just keep in mind that you can
use the GetParentFolder method to spare yourself some of the gyrations below to go back up the directory structure.
<%
' newpath will be the directory name passed in the URL when we
' link to another directory
newpath = request.querystring("newpath")
folderspec = "/users/kathi/asp/samples/"
' we want to add a forward slash to the new path if the
' value of newpath isn't a zero-length string
if newpath<>"" then
folderspec = folderspec & newpath & "/"
end if
' now we will create our filesystem object
Set fs = CreateObject("Scripting.FileSystemObject")
' now we will define the folder and subfolders
Set folder = fs.GetFolder(server.mappath(folderspec))
Set subfolder = folder.subfolders
' let's return some HTML code here
response.write "<h2>FOLDERS"
' Now we've got to take the path represented by the variable
' newpath and trim off the last subdirectory so we can use that
' as a link to the directory above it. We will do that first
' checking if there is, indeed, a value for newpath, and if there
' is, we will search for a forward slash in the string, starting
' from the end of the string, and use that position as a pointer
' so we know how many characters to peel off of the string "newpath".
' Since the pointer is at the position of the forward slash and we
' don't want the forward slash to remain, we'll subtract 1 from it.
If newpath<>"" then
Pointer = instrrev(newpath,"/")
' if the forward slash is found, we'll create the link to the
' directory above the current one.
If Pointer>0 then
UpOneLevel = Left(newpath,Pointer-1)
response.write "<a href='" & request.servervariables("script_name")
response.write "?newpath=" & UpOneLevel & "'>Up One Level</a><br>"
' Now let's make provisions where the forward slash isn't found
Else
response.write "<a href='" & request.servervariables("script_name")
response.write "'>Up One Level</a><br>"
End If
End If
' Now we can go through and add links to all the subfolders
For Each newfolder in subfolder
response.write "<A HREF='"
response.write request.servervariables("script_name")
' This is where I ran into problems...if newpath isn't a zero-length
' string, we won't want to add a forward slash, but we do want to add
' it if the value of newpath isn't a zero-length string
If newpath="" then
response.write "?newpath=" & newpath & newfolder.name
Else
response.write "?newpath=" & newpath & "/" & newfolder.name
End If
response.write "'>"
response.write newfolder.name & " - " & newfolder.datecreated
response.write "</A> "
Next
' Ok, time to go grab those files in the current directory
' First we'll do a bit of HTML
response.write "</blockquote><hr>"
response.write "<h2>FILES</h2><blockquote>"
Set filelist = folder.files
' iterate through the files and make them links
For Each file in filelist
response.write "<A HREF='"
response.write folderspec & file.name
response.write "'>"
response.write file.name & " - " & file.datecreated
response.write "</A><br>"
Next
' Now let's clean up after ourselves - destroy those objects
set folder=nothing
set subfolder=nothing
set filelist=nothing
set fs=nothing
' a little bit more HTML formatting and we're done!
response.write "</blockquote><hr>"
%>
|