You can list files in a directory, but you want to list files in all directories

I have this script to display all the files in the directory where the script is located, but I will have a directory that will contain several folders with documents in each.

What I represent is a web page that dynamically lists all folders by name, and then when you click on it, a list of related files is displayed.

I want to do this because additional folders and files will be added to this directory.

This is a script. I have something that will work IF I put it in every folder, so it is not fully dynamic.

<h3>Resources/Documents</h3>
<ul>
    <%
        Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject")
        Set MyFiles=MyDirectory.GetFolder(Server.MapPath("documents/standard_14"))
        For each filefound in MyFiles.files
    %>
    <li>
    <a href="documents/standard_14/<% =filefound.Name %>" target="blank"><% =filefound.Name %></a>
    </li>

    <% Next %>
</ul>

I am not familiar with ASP at all - any help is appreciated.

+3
1
<% ListFolderContents(Server.MapPath("/path/to/main/folder")) %>
<% 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("<h2>"& folder.Name &"</h2>")

     'Display a list of sub folders.

     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next

     'Display a list of files.
Response.Write("<ul>")
    for each item in folder.Files
       url = MapURL(item.path)
        Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
    next
        Response.Write("</ul>")
   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 %>
+8

Source: https://habr.com/ru/post/1730401/


All Articles