Nodejs檔案批量重全名

pengjielee發表於2019-10-24

Nodejs檔案批量重全名

const fs = require("fs");
const path = require("path");

var source_dir = "/source/";
var output_dir = "/output/";

var total = 1;

function start(source_dir) {
  fs.readdir(source_dir, function(err, files) {
    files.forEach(function(file, index) {
      var oldpath = path.join(source_dir, file);

      fs.stat(oldpath, function(err, stats) {
        if (stats.isDirectory()) {
          //readdir(oldpath);
        } else if (stats.isFile()) {
          var newpath = path.join(output_dir, file);
          var pathobj = path.parse(newpath);
          var filename = pathobj.name.replace("Questions", "DailyQuestions").replace(/\s+/, "");
          newpath = pathobj.dir + "/" + filename + "" + pathobj.ext;

          fs.copyFile(oldpath, newpath, function(err) {
            console.log(filename + " copyed");
          });
        }
      });
    });
  });
}

start(source_dir);
複製程式碼

相關文章