ASP Folder 对象

ASP Folder 对象(中文讲解)

在经典Active Server Pages(ASP)中,Folder对象是FileSystemObject(FSO)的子对象,用于访问和操作服务器上的文件夹。它提供文件夹的属性(如创建时间、文件数)和操作方法(如创建、复制、删除)。Folder对象通过FileSystemObjectGetFolder方法或Drives集合获取,适合管理目录结构的场景。以下是对ASP Folder对象的详细讲解,包括属性、方法、示例和注意事项。


1. Folder 对象概述

  • 作用Folder对象用于访问服务器上特定文件夹的属性和执行文件夹操作,如获取文件列表、创建子文件夹或移动目录。
  • 用途
  • 检查文件夹是否存在或获取文件夹信息(如大小、文件数)。
  • 管理日志目录、上传文件夹或备份目录。
  • 遍历文件夹中的文件和子文件夹。
  • 创建方式:通过FileSystemObjectGetFolder方法或Folder.SubFolders集合访问。
  • 依赖:依赖Scripting.FileSystemObject组件,默认随Windows安装。

2. 创建 Folder 对象

Folder对象无法直接创建,必须通过FileSystemObject获取:

  1. 通过 GetFolder 方法
   Set fs = Server.CreateObject("Scripting.FileSystemObject")
   Set folder = fs.GetFolder(Server.MapPath("logs"))
  • Server.MapPath:将虚拟路径转换为物理路径。
  1. 通过 Drives 或 SubFolders 集合
   Set fs = Server.CreateObject("Scripting.FileSystemObject")
   Set drive = fs.GetDrive("C")
   Set folder = drive.RootFolder ' 获取根目录

或遍历子文件夹:

   Set folder = fs.GetFolder(Server.MapPath("logs"))
   For Each subFolder In folder.SubFolders
       Response.Write("子文件夹:" & subFolder.Name & "<br>")
   Next
  • 注意:操作完成后释放对象以避免内存泄漏:
  Set folder = Nothing
  Set fs = Nothing

3. 常用属性

以下是Folder对象的常用属性(大多为只读):

  • Name:文件夹名称。
  Response.Write("文件夹名:" & folder.Name)
  • Path:文件夹的完整物理路径。
  Response.Write("路径:" & folder.Path)
  • Size:文件夹总大小(包括所有文件和子文件夹,字节)。
  Response.Write("大小:" & folder.Size & " 字节")
  • DateCreated:文件夹创建时间。
  Response.Write("创建时间:" & folder.DateCreated)
  • DateLastModified:文件夹最后修改时间。
  Response.Write("修改时间:" & folder.DateLastModified)
  • DateLastAccessed:文件夹最后访问时间。
  Response.Write("访问时间:" & folder.DateLastAccessed)
  • Files:文件夹中的文件集合(返回File对象集合)。
  Response.Write("文件数:" & folder.Files.Count)
  • SubFolders:文件夹中的子文件夹集合(返回Folder对象集合)。
  Response.Write("子文件夹数:" & folder.SubFolders.Count)
  • ParentFolder:父文件夹(返回Folder对象)。
  Set parent = folder.ParentFolder
  Response.Write("父文件夹:" & parent.Name)
  • Attributes:文件夹属性(如只读、隐藏,整数值)。
  • 0:普通文件夹
  • 1:只读
  • 2:隐藏
  • 4:系统文件夹
  • 可读写,设置属性:
    asp folder.Attributes = folder.Attributes Or 1 ' 设置为只读

4. 常用方法

以下是Folder对象的常用方法:

  • Copy(destination, overwrite):复制文件夹及其内容到指定路径。
  folder.Copy Server.MapPath("backup/logs"), True
  • Move(destination):移动文件夹及其内容到指定路径。
  folder.Move Server.MapPath("archive/logs")
  • Delete(force):删除文件夹及其内容(force=True可删除只读内容)。
  folder.Delete True
  • CreateTextFile(filename, overwrite, unicode):在文件夹中创建文本文件。
  Set file = folder.CreateTextFile("newfile.txt", True, True)

5. 示例代码

(1) 显示文件夹信息

获取并显示日志文件夹的详细信息:

<%@ Language=VBScript CodePage=65001 %>
<xaiArtifact artifact_id="43f861f3-9d7d-4a57-a4e3-b6db0dc85ea8" artifact_version_id="8cc64e0d-fc21-4978-91e8-2a262f0624ec" title="folder_info.asp" contentType="text/asp">
<%
Response.Charset = "UTF-8"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Dim folderPath
folderPath = Server.MapPath("logs")
If fs.FolderExists(folderPath) Then
    Set folder = fs.GetFolder(folderPath)
    Response.Write("<h1>文件夹信息</h1>")
    Response.Write("文件夹名:" & Server.HTMLEncode(folder.Name) & "<br>")
    Response.Write("路径:" & Server.HTMLEncode(folder.Path) & "<br>")
    Response.Write("大小:" & FormatNumber(folder.Size / 1024, 2) & " KB<br>")
    Response.Write("创建时间:" & folder.DateCreated & "<br>")
    Response.Write("修改时间:" & folder.DateLastModified & "<br>")
    Response.Write("文件数:" & folder.Files.Count & "<br>")
    Response.Write("子文件夹数:" & folder.SubFolders.Count & "<br>")
    Set folder = Nothing
Else
    Response.Write("文件夹不存在")
End If
Set fs = Nothing
%>
</xaiArtifact>
  • 输出示例
  文件夹信息
  文件夹名:logs
  路径:C:\inetpub\wwwroot\logs
  大小:123.45 KB
  创建时间:2025/10/05 09:00:00
  修改时间:2025/10/05 09:50:00
  文件数:5
  子文件夹数:2
(2) 列出文件夹内容

显示文件夹中的文件和子文件夹:

<%@ Language=VBScript CodePage=65001 %>
<xaiArtifact artifact_id="8701b3c3-cab4-47b7-aa6e-2d7c51e420b0" artifact_version_id="fc111740-8d10-41fe-9a36-349f7ad54738" title="folder_contents.asp" contentType="text/asp">
<%
Response.Charset = "UTF-8"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Dim folderPath
folderPath = Server.MapPath("logs")
If fs.FolderExists(folderPath) Then
    Set folder = fs.GetFolder(folderPath)
    Response.Write("<h1>文件夹内容</h1>")
    Response.Write("<h2>文件</h2><ul>")
    For Each file In folder.Files
        Response.Write("<li>" & Server.HTMLEncode(file.Name) & " (" & FormatNumber(file.Size / 1024, 2) & " KB)</li>")
    Next
    Response.Write("</ul><h2>子文件夹</h2><ul>")
    For Each subFolder In folder.SubFolders
        Response.Write("<li>" & Server.HTMLEncode(subFolder.Name) & "</li>")
    Next
    Response.Write("</ul>")
    Set folder = Nothing
Else
    Response.Write("文件夹不存在")
End If
Set fs = Nothing
%>
</xaiArtifact>
  • 输出示例
  文件夹内容
  文件
  - access.log (12.34 KB)
  - error.log (5.67 KB)
  子文件夹
  - archive
  - backup
(3) 复制文件夹

复制日志文件夹到备份目录:

<%@ Language=VBScript CodePage=65001 %>
<xaiArtifact artifact_id="51847ff0-969d-47b1-97e3-07cb1fdb229d" artifact_version_id="225c30dc-e053-4c3d-853d-921d1a301a87" title="folder_copy.asp" contentType="text/asp">
<%
Response.Charset = "UTF-8"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Dim folderPath
folderPath = Server.MapPath("logs")
If fs.FolderExists(folderPath) Then
    Set folder = fs.GetFolder(folderPath)
    folder.Copy Server.MapPath("backup/logs"), True
    Response.Write("文件夹已复制到备份目录")
    Set folder = Nothing
Else
    Response.Write("源文件夹不存在")
End If
Set fs = Nothing
%>
</xaiArtifact>
(4) 创建文件到文件夹

在文件夹中创建新文件:

<%@ Language=VBScript CodePage=65001 %>
<xaiArtifact artifact_id="007bc633-5ff2-4a9a-9214-6cfa323d9232" artifact_version_id="443a4ed6-ee0a-49a3-bc40-bd91c7520264" title="folder_create_file.asp" contentType="text/asp">
<%
Response.Charset = "UTF-8"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Dim folderPath
folderPath = Server.MapPath("logs")
If fs.FolderExists(folderPath) Then
    Set folder = fs.GetFolder(folderPath)
    Set file = folder.CreateTextFile("newlog.txt", True, True)
    file.WriteLine("[" & Now() & "] 新日志文件")
    file.Close
    Response.Write("新文件已创建")
    Set file = Nothing
    Set folder = Nothing
Else
    Response.Write("文件夹不存在")
End If
Set fs = Nothing
%>
</xaiArtifact>

6. 注意事项

  1. 权限
  • Folder对象操作需要服务器对文件夹的读写权限(IIS用户,如IUSR)。
  • 在IIS中配置应用程序池权限,确保可访问目标路径。
  1. 中文编码
  • 使用<%@ Language=VBScript CodePage=65001 %>Response.Charset = "UTF-8"支持中文。
  • NamePath输出使用Server.HTMLEncode
    asp Response.Write(Server.HTMLEncode(folder.Name))
  1. 安全性
  • 验证文件夹路径,防止路径遍历攻击:
    asp folderPath = Replace(Request.QueryString("folder"), "..", "")
  • 避免向用户暴露敏感路径(如folder.Path)。
  • 对只读文件夹操作需设置force=True(如Delete True)。
  1. 性能
  • 遍历大文件夹(FilesSubFolders)可能影响性能,尽量限制范围。
  • 释放Folder对象(Set folder = Nothing)避免内存泄漏。
  1. 错误处理
  • 使用On Error Resume Next捕获操作错误:
    asp On Error Resume Next Set folder = fs.GetFolder(Server.MapPath("logs")) If Err.Number <> 0 Then Response.Write("错误:" & Err.Description) End If On Error GoTo 0
  • 检查FolderExists避免操作不存在的文件夹。
  1. 限制
  • Folder对象不直接处理文件内容,需通过CreateTextFileFiles集合结合TextStream
  • 删除文件夹会删除所有内容,操作需谨慎。
  1. 调试
  • 使用fs.FolderExists检查文件夹存在。
  • 输出Folder属性(如SizeFiles.Count)验证信息。

7. 与Request/Response/Application/Session的关系

  • Request:提供用户输入(如文件夹路径):
  folderPath = Server.MapPath(Request.QueryString("folder"))
  Set folder = fs.GetFolder(folderPath)
  • Response:输出Folder信息:
  Response.Write("文件夹大小:" & folder.Size)
  • Application:存储全局文件夹路径:
  Application("logFolder") = Server.MapPath("logs")
  • Session:记录用户操作的文件夹:
  Session("lastFolder") = folder.Name

8. 总结

ASP的Folder对象是FileSystemObject的子对象,用于访问和操作服务器上的文件夹,提供属性(如大小、文件数)和方法(如复制、删除)。适合管理日志目录、备份等场景。注意权限、中文编码、安全性和错误处理,确保代码健壮。更多细节可参考微软官方文档中的Folder对象说明。

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注