遍历指定目录下的所有文件夹和文件

时间:2021-10-13 12:26:53

1.遍历指定目录下的所有文件夹和文件并输出

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script>
function searchFiles(obj){
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var f = fso.GetFolder(document.all.fixfolder.value);
    var fc = new Enumerator(f.files);
    var s = "";
    for (; !fc.atEnd(); fc.moveNext())
        {
            s += fc.item();
            s += "<br/>";
        }
        fk = new Enumerator(f.SubFolders);
        for (; !fk.atEnd(); fk.moveNext())
        {
        s += fk.item();
        s += "<br/>";
        }
        textarea.innerHTML = s;
}
</script>
</head>
<body bgcolor="#FFFFFF">
指定文件夹:<input type="text" name="fixfolder" value="D:\TDDOWNLOAD">
<input type="button" value ="搜索" onclick="searchFiles(this)">
<table >
<tr>
<td id = "textarea">
</td>
</tr>
</table>
</body>
</html>

 

2.遍历指定目录下的所有文件夹和文件并输出指定的文件类型

<script type="text/jscript">
  var Unity3dList = "";
  function GetUnity3dlist() {
     var txt;
     var stream = "";
     var fso = new ActiveXObject("Scripting.FileSystemObject");
     var fldr = fso.GetFolder("D:\\add");
alert(fldr);
     fc = new Enumerator(fldr.files);
     for (; !fc.atEnd(); fc.moveNext())//枚举所有文件
     {
         s = fc.item();   //取文件对象 
         alert(s.type);
         if (s.type == "HTML 文档" || s.type == "360 se HTML Document") {   //判断是否是“HTML 文档” 和“360 se HTML Document”
             f = fso.GetFile(s);
             ts = f.OpenAsTextStream(1, -2);
             txt = ts.ReadAll();   //读全文
             ts.Close();
             txttxt = txt.toUpperCase();

             titl = s.name;
             start = txt.indexOf("<TITLE>", 0);
             if (start != -1) {
                 start += 7;
                 end = txt.indexOf("</TITLE>", start);
                 if (end != -1)
                     titl = txt.substr(start, end - start);
             }
             //document.write("<a href=\"" + s + "\">" + titl + "</a><br>");
             Unity3dList += titl + "|";

               }
         document.write(Unity3dList);
     }
}

  function ReadUnity3d() {
     var stream = Unity3dList;
     var arr = new Array();
     arr = stream.split("|");
     for (var i = 0; i < arr.length-1; i++) {
          alert(arr[i].toString());
  }
}
</script>

<input type="button" value ="搜索" onclick="GetUnity3dlist()">