首页 > 其他 > 详细

node批量修改文件名称

时间:2020-02-27 22:38:44      阅读:96      评论:0      收藏:0      [点我收藏+]
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`);

  

node批量修改文件名称

原文:https://www.cnblogs.com/qianxundaozhu/p/12374925.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!