openks / learn-vue

自定义组件文档
https://openks.github.io/learn-vue
0 stars 0 forks source link

20190529_nodejs批量修改文件名 #124

Open openks opened 5 years ago

openks commented 5 years ago

文件名需要批量修改 想到了用nodejs进行处理

openks commented 5 years ago
//renameFile.js
var fs = require('fs')

var PATH = './test'; // 目录

//  遍历目录得到文件信息
function walk (path, callback) {
  var files = fs.readdirSync(path)
  files.forEach(function (file) {
    if (fs.statSync(path + '/' + file).isFile()) {
      callback(path, file)
    }else {
      walk(path + '/' + file, callback)
    }
  })
}

// 修改文件名称
function rename (oldPath, newPath) {
  fs.rename(oldPath, newPath, function (err) {
    if (err) {
      throw err
    }
  })
}

args = process.argv.slice(2)//从数组第二个开始返回
if ( args[0]==="in" ){
    walk(PATH, function (path, fileName) {
        var oldPath = path + '/' + fileName, // 源文件路径
          newPath = path + '/' + fileName + '.ddd'; // 新路径
        rename(oldPath, newPath)
      })
} else {
    walk(PATH, function (path, fileName) {
        var oldPath = path + '/' + fileName, // 源文件路径
        newPath = path + '/'+ fileName.replace(".ddd",""); // 新路径
        rename(oldPath, newPath)
      })
}

// 运行 // node renameFile.js in // node renameFile.js out