注册 登录
编程论坛 ASP技术论坛

如何实现目录遍历所有根目录下的文件?

tmwz2005 发布于 2008-06-02 20:06, 1028 次点击
我想清除数据库中不存在而空间上又有的图片文件,思路是这样的

遍历根目录下的每个文件夹,设置发现文件的标志为0,如果在遍历这个子文件夹的时候发现了文件,并且文件是有用的,则设置发现文件的标志为1,当这个子目录遍历完毕的时候,判断下文件发现标志,如果还为0则删除该子文件夹。
继续循环即可完成对所有子文件夹的遍历。

请问代码该如何写啊?求助一下各位了,感激不尽!
2 回复
#2
hxfly2008-06-02 21:29
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("<li><b>" & folder.Name & "</b> - " _
       & folder.Files.Count & " files, ")
     if folder.SubFolders.Count > 0 then
       Response.Write(folder.SubFolders.Count & " directories, ")
     end if
     Response.Write(Round(folder.Size / 1024) & " KB total." _
       & vbCrLf)



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



     'Display a list of sub folders.



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



     'Display a list of files.



     for each item in folder.Files
       url = MapURL(item.path)
       Response.Write("<li><a href=""" & url & """>" & item.Name & "</a> - " _
         & item.Size & " bytes, " _
         & "last modified on " & item.DateLastModified & "." _
         & "</li>" & vbCrLf)
     next



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



     Response.Write("</li>" & 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



转载
#3
tmwz20052008-06-03 10:43
多谢楼上的,
我不是很懂,看来还是得好好学学才行.
1