美文网首页
node 批量 修改 文件 后缀名

node 批量 修改 文件 后缀名

作者: 々柯童鞋々 | 来源:发表于2019-08-24 15:50 被阅读0次

1、公司要求微信小程序 转 字节跳动小程序,字节跳动官方没出搬家工具,于是自行研究 node fs 模块,批量修改文件后缀,达到基本效果。

let fs = require('fs');//引用文件系统模块
    
let PATH = `./app_zijietiaodong/`;//当前文件夹

let ext = {
    readFileList: function(path, filesList) {
        filesList = filesList || [];
        let files = fs.readdirSync(path);
        files.forEach(function (filename, index) {
            //var stat = fs.statSync(path + filename);//读取的文件信息
            if (fs.statSync(path + filename).isDirectory()) {//isDirectory 判断是不是目录
                //递归读取文件
                ext.readFileList(`${path}${filename}/`, filesList);
            } else {
                filesList.push({
                    path,//路径
                    filename,//名字
                });
            }
        })
        return filesList
    },
    //修改文件名称
    rename: function(oldPath, newPath, filename, newSuffixFile) {
        fs.rename(oldPath, newPath, function(err) {
            if (err) {
                throw err;
            }
            console.log(`${filename} 修改为 => ${newSuffixFile}`)
        });
    },
    //批量修改文件名称
    getChangeFiles: function (path, oldSuffix, newSuffix) {
        if(!oldSuffix && !newSuffix){
            console.log(`后缀未设置`);
        }
        this.readFileList(path).forEach((item) => {
            if(item.filename.indexOf(oldSuffix) > -1){
                console.log(item.filename)
                let oldPath = item.path + item.filename,
                newSuffixFile = item.filename.split(oldSuffix)[0] + newSuffix,
                newPath = item.path + newSuffixFile
                ext.rename(oldPath, newPath, item.filename, newSuffixFile);
            }
        });
    }
}

ext.getChangeFiles(PATH, `.wxml`, `.ttml`);

githab 先不更新了

未完待续....

相关文章

网友评论

      本文标题:node 批量 修改 文件 后缀名

      本文链接:https://www.haomeiwen.com/subject/xurqectx.html