Created
October 3, 2013 15:22
-
-
Save kandelakig/6811632 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var args = process.argv | |
var dictFile = args[2] || 'image_names.csv' | |
var filesDir = args[3] || '' | |
var fs = require('fs') | |
var dictStr = fs.readFileSync(dictFile, { | |
encoding: 'utf8' | |
}) | |
var dict = dictStr.split('\n').slice(1).filter(function(row) { | |
return row.length | |
}).map(function(row) { | |
return row.split(',') | |
}).map(function(row) { | |
return { | |
old: row[0], | |
new: row[1] | |
} | |
}) | |
console.log(dict); | |
function findAndRename(img) { | |
fs.rename(filesDir + img.old, filesDir + img.new, function(err) { | |
if (err) { | |
console.error('Could not rename file "' + filesDir + img.old + '"; Cause: ', err) | |
fail++ | |
} else { | |
success++ | |
} | |
if (success + fail == fileCount) { | |
console.info('Successfully renamed ' + success + ' files;\n' + fail + ' failures;') | |
} | |
}) | |
} | |
var fileCount = dict.length | |
var success = 0 | |
var fail = 0 | |
dict.forEach(findAndRename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment