ASP FileSystem

FileSystemObject 的文件、文件夹、驱动器、文本流操作。

Intermediate
Key PointsFileSystemObject 是操作磁盘的入口:它能建删文件目录、读写文本、遍历磁盘。

创建与常用方法

ASP
<%
    Dim fso
    Set fso = Server.CreateObject("Scripting.FileSystemObject")

    ' 判断存在
    Response.Write fso.FileExists(Server.MapPath("/data/a.txt")) & "<br>"
    Response.Write fso.FolderExists(Server.MapPath("/data")) & "<br>"

    ' 创建文件夹
    If Not fso.FolderExists(Server.MapPath("/logs")) Then
        fso.CreateFolder Server.MapPath("/logs")
    End If

    ' 复制 / 移动 / 删除
    fso.CopyFile Server.MapPath("/data/a.txt"), Server.MapPath("/data/b.txt")
    fso.MoveFile Server.MapPath("/data/b.txt"), Server.MapPath("/backup/b.txt")
    fso.DeleteFile Server.MapPath("/data/a.txt"), True      ' True = 只读也删

    Set fso = Nothing
%>

取文件信息

ASP
<%
    Dim f
    Set f = fso.GetFile(Server.MapPath("/data/report.pdf"))

    Response.Write "名称:" & f.Name & "<br>"
    Response.Write "大小:" & f.Size & " 字节<br>"
    Response.Write "创建:" & f.DateCreated & "<br>"
    Response.Write "修改:" & f.DateLastModified & "<br>"
    Response.Write "类型:" & f.Type & "<br>"
    Response.Write "父目录:" & f.ParentFolder.Path
%>
Output
名称:report.pdf
大小:184320 字节
创建:2026/10/1 9:12:03
修改:2026/11/2 17:40:55
类型:PDF 文档
父目录:C:\inetpub\wwwroot\data

遍历目录

ASP
<%
    Dim folder, file, sub_
    Set folder = fso.GetFolder(Server.MapPath("/data"))

    Response.Write "<h3>文件</h3>"
    For Each file In folder.Files
        Response.Write file.Name & " — " & file.Size & " 字节<br>"
    Next

    Response.Write "<h3>子目录</h3>"
    For Each sub_ In folder.SubFolders
        Response.Write sub_.Name & "<br>"
    Next
%>
Output
文件
report.pdf — 184320 字节
notes.txt — 1024 字节
子目录
archive
temp

读写文本

ASP
<%
    Dim ts
    ' 写(覆盖)
    Set ts = fso.CreateTextFile(Server.MapPath("/logs/app.log"), True)
    ts.WriteLine Now() & " 启动"
    ts.Close

    ' 追加
    Set ts = fso.OpenTextFile(Server.MapPath("/logs/app.log"), 8, True)
    ts.WriteLine Now() & " 追加一行"
    ts.Close

    ' 读全部
    Set ts = fso.OpenTextFile(Server.MapPath("/logs/app.log"), 1)
    Response.Write ts.ReadAll
    ts.Close
    Set ts = Nothing
%>
Output
2026/11/3 14:20:01 启动
2026/11/3 14:25:09 追加一行
ModeConstantMeaning
Read1ForReading
Write2ForWriting,覆盖原内容
Append8ForAppending,写到末尾

路径拼接

ASP
<%
    Dim p
    p = fso.BuildPath(Server.MapPath("/logs"), "2026-11-03.log")
    Response.Write p & "<br>"
    Response.Write fso.GetParentFolderName(p) & "<br>"
    Response.Write fso.GetBaseName(p) & "<br>"
    Response.Write fso.GetExtensionName(p)
%>
Output
C:\inetpub\wwwroot\logs\2026-11-03.log
C:\inetpub\wwwroot\logs
2026-11-03
log

Use BuildPath instead of concatenating "\": it handles duplicated or missing separators. Hand-building gives \\ when the base already ends in a backslash — Windows usually tolerates it, but it is sloppy.

Never build file paths straight from Request. fso.DeleteFile Server.MapPath(Request("f")) lets an attacker delete system files with ../../windows/system32/x.dll. Whitelist or constrain paths to a fixed directory with BuildPath.

权限问题

Text
IIS 以 IUSR(或应用池标识)运行,默认只有读权限。
要写文件,必须给目标目录加写权限:
  右键目录 → 属性 → 安全 → 编辑 → 添加 IUSR → 勾选「修改」

Without write permission you get Permission denied (error 70). Granting Everyone full control is the wrong reflex; give only IUSR or the app-pool identity the minimum it needs.

Friends & Partners