可以列出目录中的文件,但希望列出所有目录中的文件

时间:2010-01-28 15:07:12

标签: file-io asp-classic

我有这个脚本来显示脚本所在目录中的所有文件,但是我将有一个包含多个文件夹的目录,每个文件夹都包含文档。

我设想的是一个网页,它按名称动态列出所有文件夹,然后当您点击它时,它会显示链接的文件列表。

我想这样做,因为这个目录会添加额外的文件夹和文件。

这是我可以使用的脚本,如果我把它放在每个文件夹中,所以它不是完全动态的。

<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>

我根本不熟悉ASP - 感谢任何帮助。

1 个答案:

答案 0 :(得分:9)

<% 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 %>
相关问题