ExifRename.js
/* global process */
var fs = require('fs');
var path = require("path");
var ExifImage = require('exif').ExifImage;
var baseImgDir = process.argv[2] || '.';
function walk(imgDir, callback) {
fs.readdir(imgDir, function (err, files) {
if (err) {
callback(err, null, null);
return;
}
files.forEach(function (fileName) {
var fp = path.join(imgDir, fileName);
if (fs.statSync(fp).isDirectory()) {
console.log('Directory: ' + fp);
walk(fp, callback);
} else {
callback(null, imgDir, fileName);
}
});
});
}
function imgFileRename(imgDir, fileName) {
var newfilename = '';
var fp = path.join(imgDir, fileName);
var newfp;
new ExifImage({ image: fp }, function (err, image) {
if (err) {
console.log(fp);
console.log('ExifImageError: ' + err.message);
return;
}
newfilename = image.exif.CreateDate.replace(/:/g, '').replace(/ /g, '_');
newfilename += '_' + image.image.Model.replace(/ /g, '');
newfilename += '_' + fileName.replace('.JPG', '.jpg');
newfp = path.join(imgDir, newfilename);
fs.rename(fp, newfp, function (err) {
if (err) {
console.log(fp);
console.log('renameError: ' + err);
return;
}
console.log(fp + ' to ' + newfilename);
console.log('renamed complete');
});
});
}
walk(baseImgDir, function (err, imgDir, fileName) {
if (err) {
console.log('walkError: ' + err);
return;
}
if (fileName.indexOf('20') !== 0 && fileName.slice(-4) === '.JPG') {
imgFileRename(imgDir, fileName);
} else {
var fp = path.join(imgDir, fileName);
console.log(fp + ' no rename');
}
});
参考
http://shimz.me/blog/node-js/2601
http://qiita.com/amay077/items/cc6ee3e66040a5097230