在本地网站的目录中列出具有特定扩展名的本地文件名

时间:2018-01-16 17:41:28

标签: html jscript filesystemobject

我在本地网络硬盘上有一个网页。我想在该网页的某个目录中列出扩展名为.xlsm的文件。这是我用来在特定目录中显示所有文件的HTML。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Show Local Folder File List</title>
</head>
<body>
<div>
<script type="text/JScript">
//note this is JScript, not javascript. Thus the 'type="text/JScript"' in the script tags

function ShowFolderFileList(folderspec){
var fso, f, fc, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFolder(folderspec);
fc = new Enumerator(f.files);
s = "";
for (; !fc.atEnd(); fc.moveNext()) {
s += fc.item();
s += "<br>";
}
return s;
}

//note folder/file paths should use the local windows style backslash and be escaped '\\' :
var fileList = ShowFolderFileList('H:\\Folder1/Folder2/Folder3');
document.write(fileList);

</script>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

假设您的代码通过以下for循环打印文件名,您需要将每个值与正则表达式.+(\\.xlsm)$匹配:

for (; !fc.atEnd(); fc.moveNext()) {
    if(fc.item().match(".+(\\.xlsm)$")) {
        s += fc.item();
        s += "<br>";
    }
}

然后您的代码将仅打印以“.xlsm”

结尾的文件名