用asp遍历目录下文件的例子

2005-05-27 17:36:46 ASP Views(2301)

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

  • " & folder.Name & " - " _
             & 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("

      " & 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("

    • " & item.Name & " - " _
                  & item.Size & " bytes, " _
                  & "last modified on " & item.DateLastModified & "." _
                  & "
    • " & vbCrLf)
            next

         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
      ListFolderContents("d:/wwwroot")
         %>

  • Comments

    Leave a Comment

    Name
    Content
    Verification Type the characters you see in the picture below