需要帮助,不显示不包含任何内容的文件夹

时间:2010-01-29 16:51:26

标签: asp-classic

我有以下代码,显示每个文件夹中的所有文件夹和文件。

该脚本位于/ intranets / fs / course_outlines /文件夹中 - >的default.asp

此脚本正在查看的所有文件夹都位于/ intranets / fs / course_outlines / documents文件夹中

在下面的脚本中,它打印“documents”文件夹,我试图通过for循环运行时将其从显示中删除。

我熟悉php,所以我知道如何阻止用条件打印

文件

文件夹但我不熟悉ASP

此外,脚本打印出的格式为以下格式:

<h2>Folder name</h2>
<ul>
<li><a href="path/to/file">File name</a></li>
<li><a href="path/to/file">File name</a></li>
etc..
</ul>

<h2>Folder name</h2>
<ul>
<li><a href="path/to/file">File name</a></li>
<li><a href="path/to/file">File name</a></li>
etc..
</ul>

etc..

我遇到了一些无效的html,这与我应用于每个文件夹的jQuery手风琴效果相冲突。它的内容。因此,在打印最后一个文件夹&amp;它的内容非常有用。

先谢谢你。

<h1>Course Outlines</h1>
<% ListFolderContents(Server.MapPath("/intranets/fs/course_outlines/documents")) %>
<% 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>" & vbCrLf)

     'Display a list of sub folders.

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

    'Display a list of files.
  Response.Write("<ul>" & vbCrLf)
    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)
    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 %>

它的外观截图:http://grab.by/23Fq

它输出的一些html的样本

<h1>Course Outlines</h1> 
    <h2>documents</h2> 
<h2>APPROVED-2009-Business</h2> 
<ul> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Business/BU 111 - Nagle.doc">BU 111 - Nagle.doc</a></li> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Business/BU 112 - Ciccarelli.doc">BU 112 - Ciccarelli.doc</a></li> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Business/BU 114 - Testa H-Cronauer.doc">BU 114 - Testa H-Cronauer.doc</a></li> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Business/BU 115 - Testa H-Cronauer.doc">BU 115 - Testa H-Cronauer.doc</a></li> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Business/TT 251-252-253-254 - Sutton.doc">TT 251-252-253-254 - Sutton.doc</a></li> 
</ul> 
<h2>APPROVED-2009-Humanities</h2> 
<ul> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Humanities/ED 100 - List.doc">ED 100 - List.doc</a></li> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Humanities/ED 110 - List.doc">ED 110 - List.doc</a></li> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Humanities/ED 150 - Kelley.doc">ED 150 - Kelley.doc</a></li> 
</ul> 
<h2>APPROVED-2009-MAHPES</h2> 
<ul> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-MAHPES/HE 121 - Aragon-Dommer A.doc">HE 121 - Aragon-Dommer A.doc</a></li> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-MAHPES/HE 128 - Howell.doc">HE 128 - Howell.doc</a></li> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-MAHPES/HE 130 - Aragon.doc">HE 130 - Aragon.doc</a></li> 
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-MAHPES/SC 261 - Mezik.doc">SC 261 - Mezik.doc</a></li> 
</ul>

显示空<ul></ul>

它在ul中没有任何内容,所以它似乎再次运行才能快速知道,你能帮助w /代码来防止它。

<h2>Last Folder to display</h2>
<ul>
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Social Science/SS 240 - Verri.doc">SS 240 - Verri.doc</a></li>
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Social Science/SS 241 - Verri.doc">SS 241 - Verri.doc</a></li>
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Social Science/SS 245 - Elwood.doc">SS 245 - Elwood.doc</a></li>
<li><a href="/intranets/fs/course_outlines/documents/APPROVED-2009-Social Science/SS 246 - Hack Polkosnik.doc">SS 246 - Hack Polkosnik.doc</a></li>
</ul>
<ul>
</ul>

1 个答案:

答案 0 :(得分:1)

尝试这样的事情。如果子文件夹或子文件计数大于零,它仅显示文件夹:

<强>更新

<h1>Course Outlines</h1>
<% ListFolderContents Server.MapPath("/intranets/fs/course_outlines/documents"), 0 %>
<% 
sub ListFolderContents(path, level)
    dim fs, folder, file, item, url, subFiles, subFolders
    set fs = CreateObject("Scripting.FileSystemObject")
    set folder = fs.GetFolder(path)

    if folder.SubFolders.Count > 0 or folder.Files.Count > 0 then
        'Display the target folder and info.
        if level > 0 then
            Response.Write("<h2>"& folder.Name &"</h2>" & vbCrLf)
        end if

        'Display a list of sub folders.
        for each item in folder.SubFolders
            ListFolderContents item.Path, level + 1
        next

        'Display a list of files.
        Response.Write("<ul>" & vbCrLf)
        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)
    end if             
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 
%>