Folder Structure List in Classic ASP

I developed a secure ASP page for the company I work for. There is a landing (login page), which after you have been authenticated, you get to a page with links to several subpages. Each subpage has a folder structure. For example: there is a heading for the minutes of the meeting, and then under it there are also indents - links that link to PDF files containing information. There may be 3 or 4 headings with documents below.

In the original version, a PHP script was launched and would synchronize the live site on the server from the folder structure that would be simulated on the live site. So if I had a folder called Folder1 and subfolders named test1 test2 test3 .., then the live site would display them accordingly. Since the site is now in ASP and not PHP, the PHP script no longer works (since PHP does not work well with ASP).

I found a snippet on the Internet that works somewhat for what I'm trying to achieve (e.g. Folder / Subfolder / File Name structure), however, I'm currently stuck in linking files so that they open when clicked. I continue to see% 25 in the file name. I know that% 20 is the same as empty space, and since I am dealing with file and folder names that contain spaces, this seems to be my problem. I tried adding in% 20, but spaces become "% 2520".

If you look at the code below, there is a link at the bottom that calls "MapURL". I have a link commented at the moment when I was trying to figure out where% 25 came from. Does anyone have any thoughts on how to make links work?

Here is a snippet.

dim path 
path = "PATH TO THE FOLDER ON THE SERVER"

ListFolderContents(path)

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("<ul><b>" & folder.Name & "</b>") '- " _
 '  & folder.Files.Count & " files, ")
 'if folder.SubFolders.Count > 0 then
 '  Response.Write(folder.SubFolders.Count & " directories, ")
 'end if
 'Response.Write(Round(folder.Size / 1024) & " KB total." _
 '  & "</ul>" & vbCrLf)

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

 'Display a list of sub folders.

 for each item in folder.SubFolders
   ListFolderContents(item)
 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> - " _

   Response.Write("<li><a href=" & Replace(item.path," ","%") & ">" & item.Name & "</a> - " _
     & item.Name & "</a>" _
     & "</li>" & vbCrLf)
 next

 Response.Write("</ul>" & vbCrLf)
 Response.Write("</ul>" & 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
+5
2

.

  • , , . . URL- , HREF, HTML- .
  • FileSystemObject ListFolderContents(). , .
  • Folder , . .
  • HTML, , . <b> <ul>.

, . PathEncode(), URL. :

ListFolder "P:\ATH\TO\THE\FOLDER\ON\THE\SERVER"

' -- Main Functions ----------------------------------------------------
Sub ListFolder(path) 
  Dim fs, rootPath

  Set fs   = CreateObject("Scripting.FileSystemObject")
  rootPath = Replace(path, Server.MapPath("/"), "") & "\"

  ListFolderContents fs.GetFolder(path), PathEncode(rootPath)
End Sub

' ----------------------------------------------------------------------
Sub ListFolderContents(folder, relativePath)
  Dim child

  Say "<ul>"
  Say "<li><div class=""folder"">" & h(folder.Name) & "</div>"

  For Each child In folder.SubFolders
    If Not IsHidden(child) Then
      ListFolderContents child, relativePath & PathEncode(child.Name) & "/"
    End If  
  Next

  relativePath = h(relativePath)

  For Each child In folder.Files
    If Not IsHidden(child) Then
      Say "<li><a href=""" & relativePath & h(PathEncode(child.Name)) & """>" & h(child.Name) & "</a></li>"
    End If
  Next

  Say "</ul>"
End Sub

' -- Helper Functions / Shorthands ---------------------------------------
Sub Say(s)
  Response.Write s & vbNewLine
End Sub

Function h(s)
  h = Server.HTMLEncode(s)
End Function

Function PathEncode(s)
  ' this creates a more correct variant of what Server.URLEncode would do
  PathEncode = Replace(s, "\", "/")
  PathEncode = Server.URLEncode(PathEncode)
  PathEncode = Replace(PathEncode, "+", "%20")
  PathEncode = Replace(PathEncode, "%2F", "/")
  PathEncode = Replace(PathEncode, "%2E", ".")
  PathEncode = Replace(PathEncode, "%5F", "_")
End Function

Function IsHidden(File)
  IsHidden = File.Attributes And 2 = 2
End Function

  • <div class="folder">, CSS (.. ..) .
  • .
  • relativePath , - 1000 , 1000 . , .
  • , Say() h() , , .
  • URL- ( HTML-). , , , , - .
+5

, , href (""). - ( ), <a href=""" & replace(...) & """>"
, , , HTML-, href= .

+2

All Articles