开发者

Need help with not displaying folders that do not contain anything

I have the following code that displays all folders and files within each folder.

The script is within the /intranets/fs/course_outlines/ folder -> default.asp

All the folders that this script is looking at are within the /intranets/fs/course_outlines/documents folder

Within the script below, it prints the "documents" folder and I am trying to remove that from displaying when it runs thru the for loop.

I am familiar with php, so I would know how to stop that from displaying the

documents

folder from printing with a conditional but I am unfamiliar with ASP

Also, the script prints out the html to be in this format:

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

I ran into some invalid html, which conflicts with a jQuery accordion effect I have applied to each folder & its contents. So any help on it ending with the appropriate closing tag after it prints the very last folder & its contents would be very helpful.

Thank you in advanced.

<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 eac开发者_StackOverflowh 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 %>

Screenshot of how it looks: http://grab.by/23Fq

Sample of some of the html it outputs

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

It displays an empty <ul></ul>

It has nothing within the the ul's, so it seems to run once more before it knows to quick, can you help w/ the code to prevent that.

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


Try something like this. It only displays the folder if the sub-folder or sub-file count is greater than zero:

Updated:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜