ASP Folder

Folder 对象的属性、子项遍历、创建删除与大小统计。

Intermediate
Key PointsFolder 对象代表一个目录:能列出文件和子目录、统计大小、创建删除、复制移动。

属性

PropertyMeaning
Name不含路径
Path物理路径
Size含所有子目录,字节
Files直接子文件
SubFolders直接子目录
Files.Count
DateCreated
IsRootFolder盘符根目录为 True
ParentFolder

列出目录内容

ASP
<%
    Dim fso, folder, item
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(Server.MapPath("/uploads"))

    Response.Write "<h3>共 " & folder.Files.Count & " 个文件</h3>"
    Response.Write "<table border=""1"">"
    Response.Write "<tr><th>文件名</th><th>大小</th><th>修改时间</th></tr>"
    For Each item In folder.Files
        Response.Write "<tr><td>" & Server.HTMLEncode(item.Name) & "</td><td>" & _
                       FormatNumber(item.Size / 1024, 1) & " KB</td><td>" & _
                       item.DateLastModified & "</td></tr>"
    Next
    Response.Write "</table>"
%>
Output
共 2 个文件
文件名            大小        修改时间
report.pdf       180.0 KB    2026/11/2 17:40:55
notes.txt        1.0 KB      2026/11/3 9:02:11

递归统计大小

ASP
<%
    Function DirSize(folder)
        Dim f, sub_, total
        total = 0
        For Each f In folder.Files
            total = total + f.Size
        Next
        For Each sub_ In folder.SubFolders
            total = total + DirSize(sub_)
        Next
        DirSize = total
    End Function

    Response.Write FormatNumber(DirSize(folder) / 1048576, 2) & " MB"
%>
Output
12.48 MB

folder.Size already includes subdirectories, so recursion is usually unnecessary. On huge trees Size walks everything at once — slow and heavy — while your own recursion can report progress or skip directories.

创建与删除

ASP
<%
    ' 创建(父目录必须存在)
    If Not fso.FolderExists(Server.MapPath("/logs/2026")) Then
        fso.CreateFolder Server.MapPath("/logs/2026")
    End If

    ' 删除(True = 连同内容一起删)
    fso.DeleteFolder Server.MapPath("/logs/2026"), True

    ' 复制与移动
    folder.Copy Server.MapPath("/backup/uploads"), True
    folder.Move Server.MapPath("/archive/uploads")
%>

CreateFolder does not create intermediate parents: making /a/b/c when /a/b is missing raises an error. Create each level yourself, or create the parent first.

遍历子目录

ASP
<%
    Dim sub_, depth
    Sub ShowTree(f, depth)
        Response.Write String(depth * 2, " ") & "📁 " & f.Name & "<br>"
        Dim s
        For Each s In f.SubFolders
            ShowTree s, depth + 1
        Next
    End Sub

    ShowTree folder, 0
%>
Output
📁 uploads
  📁 2025
    📁 10
  📁 2026
    📁 11
Recursive traversal loops forever on symlinks or junctions. Track visited paths or cap the depth — such a bug hangs the IIS worker until timeout.

删除目录的安全问题

ASP
<%
    ' 危险:目录名来自参数
    fso.DeleteFolder Server.MapPath(Request("dir")), True

    ' 安全:固定父目录 + 名称白名单
    Dim name
    name = Request("dir")
    If name Like "[A-Za-z0-9_-]*" And InStr(name, "..") = 0 Then
        fso.DeleteFolder Server.MapPath("/uploads/" & name), True
    End If
%>

DeleteFolder with True deletes recursively. Any user influence over the path requires strict validation — a ../../ input can wipe the whole site.

Friends & Partners