这周俺们老板给我安排了个任务,给你一大批文件,现在按文件名放入对应的文件夹里,并且放好后名字都要改成index.html,数量大约是四百个,假如一个一个放那要放到手抽筋而且效率非常慢,所以只能使出我的脚本大法!
如图一共是454个文件,需要全部归类
里边是这样
这是需要放入的文件,文件名与文件夹路径对应
下面上代码
<script type="text/javascript">
var fso = new ActiveXObject("Scripting.FileSystemObject");
// var baseUrl = $("#baseUrl").val();
function test() {
// var f1 = fso.createtextfile("e:\\myjstest.txt", true);
console.log("运行了");
const baseUrl = document.getElementById("baseUrl").value;
// var f2 = fso.GetFolder(
// "e:\\dev-project\\mars3d-es5-example-new\\example\\graphic\\entity"
// );
var f2 = fso.GetFolder(baseUrl);
var underFiles = new Enumerator(f2.files);
for (; !underFiles.atEnd(); underFiles.moveNext()) {
var path = "" + underFiles.item();
//alert(fn);
// var content = readFile(fn, fso);
var f1file = fso.GetFile(path);
const path2 = cutText(path, baseUrl.length);
const path21 = getCaption(path2);
const path3 = path21.substring(0, path21.length - 5);
// const path4 =
// "e:\\dev-project\\mars3d-es5-example-new\\example\\graphic\\entity\\" +
// path3 +
// "\\index.html";
const path4 =
baseUrl +
"\\" +
cutText2(path2, path21.length) +
"\\" +
path3 +
"\\index.html";
console.log(path4);
console.log(path2);
// console.log(path3);
// console.log(cutText2(path2, path21.length));
f1file.move(path4);
}
}
function getCaption(obj) {
// const caption = document.getElementById("caption").value + "-";
// console.log(caption);
var index = obj.indexOf("\-"); //获取-后边的字符串"\e-"
obj = obj.substring(index + 1, obj.length);
return obj;
}
function cutText(path, length) {
const caption = document.getElementById("caption").value;
var text = path.slice(length + caption.length + 2, path.length);
return text;
}
function cutText2(path2, length) {
var text = path2.slice(0, path2.length - length - 1);
return text;
}
function readFile(path, fso) {
var f1 = fso.GetFile(path);
var fh = fso.OpenTextFile(f1, 1 /*reading*/);
var content = "";
while (!fh.AtEndOfStream) {
content += fh.ReadLine();
}
fh.close();
return content;
}
</script>
核心是ActiveXObject这个由微软开发的对象,基于这个对象我们能实现文件的操作,非常简洁与方便,可以随时在自己的html页面上使用