Skip to content

Instantly share code, notes, and snippets.

@geneticgrabbag
Created February 24, 2013 16:27
Show Gist options
  • Save geneticgrabbag/5024430 to your computer and use it in GitHub Desktop.
Save geneticgrabbag/5024430 to your computer and use it in GitHub Desktop.
Rename file to standard naming convention. Node.JS script to take a list of files and rename each to the format YYYYMMDD_hhmmss.ext. Used by the criminally pedantic when archiving images and movies. Requires Node.JS version 0.8 or higher.
var fs = require('fs');
var path = require('path');
var util = require('util');
var dateFormat = require('dateformat');
process.argv.forEach(function(val, index, array) {
if (index < 2) return;
if (! fs.existsSync(val)) {
console.error("File \"" + val + "\" does not exist.");
return;
}
s = fs.statSync(val);
if (!s.isFile()) {
console.error("\"" + val + "\" is not a plain file.");
return;
}
mtime = new Date(s.mtime);
var newname = dateFormat(mtime, "yyyymmdd_HHMMss") + path.extname(val);
console.log(newname);
fs.renameSync(val, newname);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment