修改文件名

  1. // 引入fs文件处理模块
  2. var fs = require("fs");
  3. var paths = ['chart-orange', 'chart-purple', 'chart-red', 'chart-yellow', 'number-blue', 'number-red', 'title-blue', 'title-red']
  4. paths.forEach((path) => {
  5. fs.readdir(path, function (err, files) {
  6. // files是名称数组
  7. files.forEach(function (filename, index) {
  8. //运用正则表达式替换oldPath中不想要的部分
  9. var oldPath = path + '/' + filename,
  10. newPath = path + '/' + `${path}-${filename}`;
  11. console.log(newPath);
  12. fs.rename(oldPath, newPath, function (err) {
  13. if (!err) {
  14. console.log(filename + '副本替换成功!')
  15. }
  16. })
  17. })
  18. })
  19. })