首页 | 免费域名 | 个人服务器 | 一流信息监控拦截系统 | 虚拟主机知识库 | ASP 空间 | ASP技术大全 | 下载中心 | 客户服务中心
  7i24 > ASP技术大全 > 代码乐园 >
    7i24 .Com  
  显示多级目录和文件的源程序

7i24.Com不停为您服务


<%@ LANGUAGE="VBScript" %>

<html>
<head>
<title>Directory List</title>
<style type="text/css">

ul.squares { list-style-type: square; }

</style>
</head>
<body bgcolor="#ffffff">
<center>

<table border=0 cellpadding=8 cellspacing=0 width=550>
<tr><td align=center colspan=2>
<hr noshade>
<font color="#cc6600" face="Arial,Helvetica" size=4><b>Directory List</b></font><hr noshade>
</td></tr>
<tr>
<td bgcolor="#cccccc" width=100> </td>
<td width=450>
<font face="Arial,Helvetica" size=2>
This script shows how to read a directory structure in ASP. The display belowshows the contents of a few directories located on this site, including anysubdirectories and their contents.
<p>
Each file is displayed as a link, so you can click on the file name to viewthat particular file.
<p>
<ul class="squares">
<% ListFolderContents (Server.MapPath("/asp")) %>
<% ListFolderContents (Server.MapPath("/dhtml")) %>
<% ListFolderContents (Server.MapPath("/js")) %>
</ul>
<p>
<font color="#cc6600"><b>How it Works</b></font>
<p>
The script makes use of the FileScriptingObject which allows you to accessfiles and directories on the server. These objects contain properties thatdescribe the file or folder name, physical path, size, last modified date andother information.
<p>
Folder objects also contain collections of other FileScriptObjects for eachsubfolder and file in that directory.
<p>
The <b>ListFolderContents</b> subroutine does the work. It requires thephysical path to the folder, which can be found using the built-in
<b>Server.MapPath()</b> function. This function takes either a relative orabsolute URL and returns the physical path to that file. For example, the URL"/graphics/image.gif" might return a path of
"D:\Inetpub\wwwroot\graphics\image.path". Either directories or files can beused.
<p>
<b>Using Recursion</b>
<p>
<b>ListFolderContents</b> first creates a Folder object using the given path.
It displays the folder name then loops through the SubFolders collection ofthat folder. For each subfolder it makes a recursive call to itself, passingthe subfolder path.
<p>
This causes it to walk down through the hierarchial directory structure anddisplay each subfolder and contents. The recursion stops down each branch whenit reaches a subfolder that has no subfolders of it's own.
<p>
<b>Mapping a Physical Path to a URL</b>
<p>
After listing all subfolders, <b>ListFolderContents</b> then loops through theFiles collection, displaying each file name to complete the contents displayfor the given folder.
<p>
Each file is displayed as a standard HTML link. Unfortunately the
<b>Server.MapPath()</b> function has no compliment to convert a physical pathback to a URL. But this is not difficult to do and the script includes afunction called <b>MapURL</b> that takes the file path and returns a valid URLthat can be used to link to it.
<p>
<b>Notes on the Example</b>
<p>
To list the entire site structure you could simply call
<b>ListFolderContents</b> once with path to the web site root at the argument.
<p>
<pre>
<ul>
<% ListFolderContents (Server.MapPath("/")) %>
</ul>
</pre>
<p>
For the example on this page however, only a few subdirectories are used.
Otherwise the display would be several pages long. So separate calls are madefor each subdirectory to be shown.
<p>
<pre>
<ul>
<% ListFolderContents (Server.MapPath("/asp")) %>
<% ListFolderContents (Server.MapPath("/dhtml")) %>
<% ListFolderContents (Server.MapPath("/js")) %>
</ul>
</pre>
<p>
<font color="#cc6600"><b>Source</b></font>
<p>
<ul>
 <li><a href="viewsource.asp?source=/asp/dirlist.asp">dirlist.asp</a>
</ul>
<p>
</font>
</td>
</tr>
<tr><td align=center colspan=2>
<hr noshade size=1>
<font face="Arial,Helvetica" size=2><a href="/" target="_top">Home</a></font></td></tr>
</table>

</center>
</body>
</html>

<% sub ListFolderContents(path)

  dim fs, folder, file, item, url

  set fs = CreateObject("Scripting.FileSystemObject")
  set folder = fs.GetFolder(path)

  Response.Write("<li><b>" & folder.Name & "</b>" & vbCrLf)
  Response.Write("<ul class=""squares"">" & vbCrLf)

  'Display sub folders.

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

  'Display files.

  for each item in folder.Files
   url = MapURL(item.path)
   Response.Write("<li><a href=""" & url & """>" & item.name & "</a>
" & vbCrLf)
  next

  Response.Write("</ul>" & vbCrLf)

 end sub

 function MapURL(path)

  dim rootPath, url

  'Convert a physical file path to a URL.

  rootPath = Server.MapPath("/")
  url = Right(path, Len(path) - Len(rootPath))
  MapURL = Replace(url, "\", "/")

 end function %>



  2002年1月14日  阅读 2882 次  发送此页给朋友  来源:    版权争议  删除

相关文章:   近期热点:

上一篇: 包含文件的得与失
下一篇: 用FileObject建立标准网站维护
返回上一层...
搜索:

(C)2004-2022 7i24.Com 保留所有权利