Skip to content

Instantly share code, notes, and snippets.

@vikrambiwal
Created October 31, 2017 13:48
Show Gist options
  • Save vikrambiwal/05a1006f5428716b596402850aff7c2c to your computer and use it in GitHub Desktop.
Save vikrambiwal/05a1006f5428716b596402850aff7c2c to your computer and use it in GitHub Desktop.
Change or rename a file.(upper case to loer)
const fs = require('fs');
const path = require('path');
if (process.argv.length <= 2) {
console.log('Usage: ' + __filename + ' path/to/directory');
process.exit(-1);
}
var directoryPath = process.argv[2];
fs.readdir(directoryPath, (err, files) => {
files.forEach(file => {
var fileName = file.replace(/(.*)\.[^.]+$/, '$1');
var newFileName =
fileName.replace(/([A-Z]+)/g, function(match1) {
return '_' + match1.toLowerCase();
}) + path.extname(file);
fs.rename(directoryPath + '/' + file, directoryPath + '/' + newFileName, function() {});
console.log(directoryPath + file);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment