Created
October 9, 2020 04:35
-
-
Save doKill/a3a8496d43dddb5ec83864ab429253aa to your computer and use it in GitHub Desktop.
文件批量重命名
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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