views:

41

answers:

1

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

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>
+1  A: 

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 
%>
RedFilter
I replaced what you had in for the sub part, I had to add Then after the conditional. It still displayed documents, since that has folders and files in it. That is where the script lives in (/intranets/fs/course_outlines), and I only want the script to scan starting at /intranets/fs/course_outlines/documentshere is a screen shot of the documents folder that contains all the sub folders that I want the script to only display: http://grab.by/23IA
Brad
added a `level` parameter so it can skip the top level (0)
RedFilter
unsuccessful, it gave me this error: http://grab.by/23S1
Brad
That's pretty self-explanatory - sorry I don't do much VB and this is one of the quirks, that it will accept a single parameter in brackets but not multiple. Try modified version.
RedFilter