You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不使用Enumerator类的情况下用JavaScript遍历FileSystemObject集合

不依赖Enumerator类遍历FileSystemObject文件集合的方法

你可以使用for...in循环来遍历FileCollection,无需依赖Enumerator类,这种方式在不支持Enumerator的JavaScript环境中同样有效。

修改后的完整代码如下:

var FSO = Server.CreateObject("Scripting.FileSystemObject");

// 引用目标目录
var Folder = FSO.GetFolder("c:\\Text");

// 引用目录下的文件集合
var FileCollection = Folder.Files;

Response.Write("JScript Method<BR>");

// 显示目录中的文件数量
Response.Write("Number of files found: " + FileCollection.Count + "<BR>");

// 使用for...in遍历文件集合
for (var fileKey in FileCollection) {
    // 确保是集合自身的文件项(避免遍历原型链属性)
    if (FileCollection.hasOwnProperty(fileKey)) {
        var fileObj = FileCollection[fileKey];
        // 输出文件路径,也可通过fileObj.Name获取文件名
        Response.Write(fileObj.Path + "<BR>");
    }
}

// 清理对象引用
FileCollection = null;
Folder = null;
delete FSO;
FSO = null;

说明

  • for...in会遍历FileCollection的所有可枚举属性,每个属性对应集合中的一个文件对象。
  • 加入hasOwnProperty判断是为了避免意外遍历到集合原型链上的额外属性,确保只处理实际的文件项。
  • 你可以通过文件对象的Path属性获取完整路径,或Name属性获取仅文件名,按需使用即可。

内容的提问来源于stack exchange,提问作者stack92

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 08:58:22