Skip to content

Instantly share code, notes, and snippets.

@dialupnoises
Created June 25, 2017 10:21
Show Gist options
  • Save dialupnoises/a27347b0871eb2021faf8ab9452a91cc to your computer and use it in GitHub Desktop.
Save dialupnoises/a27347b0871eb2021faf8ab9452a91cc to your computer and use it in GitHub Desktop.
var fs = require('fs');
// i'm fairly sure i didn't write this, but i don't remember where i got it
// performs a natural sort, so 10 comes after 2.
function naturalCompare(a, b)
{
var i, codeA, codeB = 1, posA = 0, posB = 0, alphabet = String.alphabet;
function getCode(str, pos, code) {
if (code) {
for (i = pos; code = getCode(str, i), code < 76 && code > 65;) ++i;
return +str.slice(pos - 1, i);
}
code = alphabet && alphabet.indexOf(str.charAt(pos))
return code > -1 ? code + 76 : ((code = str.charCodeAt(pos) || 0), code < 45 || code > 127) ? code
: code < 46 ? 65 // -
: code < 48 ? code - 1
: code < 58 ? code + 18 // 0-9
: code < 65 ? code - 11
: code < 91 ? code + 11 // A-Z
: code < 97 ? code - 37
: code < 123 ? code + 5 // a-z
: code - 63
}
if ((a+="") != (b+="")) for (;codeB;) {
codeA = getCode(a, posA++);
codeB = getCode(b, posB++);
if (codeA < 76 && codeB < 76 && codeA > 66 && codeB > 66) {
codeA = getCode(a, posA, posA);
codeB = getCode(b, posB, posA = i);
posB = i;
}
if (codeA != codeB) return (codeA < codeB) ? -1 : 1;
}
return 0;
}
function pad(num, size)
{
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
var stats = {};
// find only images
var files = fs.readdirSync('.').filter(function(f) {
if(['jpg','png','jpeg'].indexOf(f.split('.').slice(-1)[0].toLowerCase()) == -1)
return false;
var stat = fs.statSync(f);
stats[f] = stat;
return !stat.isDirectory();
});
// find all command line switches
var argvOptions = process.argv.slice(2);
files = files.sort(function(a, b) {
// allow sorting by time downloaded instead of filename
if(argvOptions.indexOf("time") != -1)
{
if(stats[a].birthtime > stats[b].birthtime)
return 1;
if(stats[a].birthtime < stats[b].birthtime)
return -1;
return 0;
}
else
{
return naturalCompare(a, b);
}
});
var commit = false;
// don't do anything unless the user has made sure the result was correct
if(argvOptions.indexOf("commit") != -1)
{
commit = true;
}
else
{
console.log("showing a PREVIEW. type 'sequencer commit' to actually rename things.");
}
var padTo = (files.length < 1000 ? 3 : files.length.toString().length);
// sequence
for(var i = 0; i < files.length; i++)
{
(function() {
// compute new filename
var file = files[i];
var ext = file.split('.').slice(-1)[0].toLowerCase();
var newFile = pad(i + 1, padTo) + "." + ext;
if(newFile != file && fs.existsSync(newFile))
throw new Error("File exists: " + file + " -> " + newFile);
// if we're supposed to do something, rename the file
if(commit)
{
fs.rename(file, newFile, function(err) {
if(err) throw err;
console.log(file + " -> " + newFile);
});
}
else
{
console.log(file + " -> " + newFile);
}
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment