程序预览
![](https://img2020.cnblogs.com/blog/1777435/202008/1777435-20200805150043394-2072028555.png)
![](https://img2020.cnblogs.com/blog/1777435/202008/1777435-20200805150208079-949598953.png)
![](https://img2020.cnblogs.com/blog/1777435/202008/1777435-20200805150327038-1493409835.png)
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"
}
}