node根据excel批量更名

node根据excel批量更名

程序预览

index.js

var xlsx = require('node-xlsx');
var fs = require('fs');
process.stdin.setEncoding('utf8');

// This function reads only one line on console synchronously. After pressing `enter` key the console will stop listening for data.
function readlineSync() {
    return new Promise((resolve, reject) => {
        process.stdin.resume();
        process.stdin.on('data', function (data) {
            process.stdin.pause(); // stops after one line reads
            resolve(data);
        });
    });
}

// entry point
async function main() {
    console.log('请输入或拖入表格路径,输入后按回车键');
    let sheetsInput = await readlineSync();
    console.log('请输入或拖入待更名文件的文件夹路径,输入后按回车键');
    let zipDirPathInput = await readlineSync();
    console.log("输入的信息是:",sheetsInput,zipDirPathInput);







    var sheets = xlsx.parse(sheetsInput.trim());//xlsx.parse("C:\\Users\\Administrator\\Desktop\\1.xlsx");//获取到所有sheets
    var zipDirPath=zipDirPathInput.trim();//"C:\\Users\\Administrator\\Desktop\\zipdir";
    let totalNum=0;
    sheets.forEach(function(sheet,index){
        //console.log(sheet['name']);

        if(index==0){
            for(var rowId in sheet['data']){
                var row=sheet['data'][rowId];
                //console.log(rowId);
                //console.log(row);
                let thisName=row[0];
                let thisCode=row[1];
                //console.log(thisCode);

                let needChangeFliePath=zipDirPath + '/' + thisCode+".zip";
                let newPathName=zipDirPath + '/' + thisName+"_"+thisCode+"_原"+thisCode+".zip";
                fs.stat(needChangeFliePath, function(err, stat){
                    if(stat&&stat.isFile()) {
                        //console.log('文件存在');
                        //console.log(needChangeFliePath);
                        fs.rename(needChangeFliePath, newPathName, function(err) {
                            if (err) {
                                throw err;
                            }else {
                                totalNum++;
                                //let info=zipDirPath + '/' + thisName+thisCode+"_原"+thisCode+".zip";
                                console.log("已处理"+totalNum+" "+newPathName);
                            }
                        });
                    } else {
                        console.log(needChangeFliePath+',文件未匹配到或已处理,不需要处理');
                    }
                });

            }
        }

    });



}

main();

package.json


{
  "name": "1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node-xlsx": "^0.15.0"
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值