Created
July 1, 2014 19:21
-
-
Save eush77/df9422eef4f16a421d28 to your computer and use it in GitHub Desktop.
Small script that renames files with percent-encoded names (sometimes Curl downloads them)
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/nodejs | |
var fs = require('fs'); | |
var util = require('util'); | |
var decodeFiles = function(files) { | |
util.puts(''); | |
var message = '\rRenaming... '; | |
var count = files.length; | |
files.forEach(function(file, index) { | |
index += 1; | |
util.print(message + index + '/' + count); | |
fs.rename(file.old, file.new, function(err) { | |
if (err) { | |
util.puts('\n'); | |
throw err; | |
} | |
}); | |
}); | |
util.puts(message + ' \033[32mDone\033[0m'); | |
}; | |
var files = fs.readdirSync('.').map(function(filename) { | |
var newFilename = decodeURIComponent(filename).replace(/\//g, '-'); | |
return filename == newFilename ? null : {old: filename, new: newFilename}; | |
}).filter(Boolean); | |
// Here the interactivity starts | |
if (!files.length) { | |
util.puts('Nothing to decode.'); | |
process.exit(); | |
} | |
util.puts('The following files will be renamed:'); | |
files.forEach(function(file) { | |
util.puts(' "' + file.old + '"\n -> "\033[34m' + file.new + '\033[0m"\n'); | |
}); | |
!function askToProceed() { | |
var prompt = function() { | |
util.print('Proceed (y/n)? '); | |
}; | |
prompt(); | |
process.stdin.on('data', function(answer) { | |
answer = String(answer).trim().toLowerCase(); | |
if (answer == 'y') { | |
process.stdin.pause(); | |
decodeFiles(files); | |
} | |
else if (answer == 'n') { | |
process.exit(); | |
} | |
else { | |
prompt(); | |
} | |
}); | |
process.stdin.resume(); | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment