需要重命名一大批文件,将所有文件名小写,好多小工具都有木马病毒,索性斜段小脚本搞定
/* global: require */
'use strict'
var fs = require('fs')
var path = require('path')
// var copyFile = function(srcPath, tarPath, cb) {
// var rs = fs.createReadStream(srcPath)
// rs.on('error', function(err) {
// if (err) return console.log('read error', srcPath)
// if (cb) cb(err)
// })
// var ws = fs.createWriteStream(tarPath)
// ws.on('error', function(err) {
// if (err) return console.log('write error', tarPath)
// if (cb) cb(err)
// })
// ws.on('close', function(ex) {
// if (cb) cb(ex)
// })
// rs.pipe(ws)
// }
function copyFile(src, dist, cb) {
console.log('cp ', dist)
fs.writeFileSync(dist, fs.readFileSync(src))
if (cb) cb()
}
var copyFolder = function(srcDir, tarDir, cb) {
fs.readdir(srcDir, function(err, files) {
var count = 0
var checkEnd = function() {
if (++count == files.length && cb) cb()
}
if (err) return checkEnd()
files.forEach(function(file) {
var srcPath = path.join(srcDir, file)
var tarPath = path.join(tarDir, String(file).toLowerCase())
fs.stat(srcPath, function(err, stats) {
if (stats.isDirectory()) {
console.log('mkdir', tarPath)
if (!fs.existsSync(tarPath)) {
fs.mkdir(tarPath, function(err) {
if (err) return console.log(err)
copyFolder(srcPath, tarPath, checkEnd)
})
} else {
copyFolder(srcPath, tarPath, checkEnd)
}
} else {
copyFile(srcPath, tarPath, checkEnd)
}
})
})
//为空时直接回调
if (files.length === 0 && cb) cb()
})
}
// 使用时
copyFolder('../142000', '../142001', function(err) {
if (err) return;
console.log('====== finish ========')
//continue
})