Skip to content

Instantly share code, notes, and snippets.

@doKill
Created October 9, 2020 04:35
Show Gist options
  • Save doKill/a3a8496d43dddb5ec83864ab429253aa to your computer and use it in GitHub Desktop.
Save doKill/a3a8496d43dddb5ec83864ab429253aa to your computer and use it in GitHub Desktop.
文件批量重命名
var fs = require('fs');
var PATH = './wzz'; // 目录
// 遍历目录得到文件信息
function walk(path, callback) {
var files = fs.readdirSync(path);
files.forEach(function(file){
if (fs.statSync(path + '/' + file).isFile()) {
callback(path, file);
}
});
}
// 修改文件名称
function rename (oldPath, newPath) {
fs.rename(oldPath, newPath, function(err) {
if (err) {
throw err;
}
});
}
// 运行
walk(PATH, function (path, fileName) {
var oldPath = path + '/' + fileName, // 源文件路径
newPath = path + '/'+ fileName.split('第')[1]; // 新路径
rename(oldPath, newPath);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment