Skip to content

Instantly share code, notes, and snippets.

@leotm
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leotm/1980d616da01381c7cdf to your computer and use it in GitHub Desktop.
Save leotm/1980d616da01381c7cdf to your computer and use it in GitHub Desktop.
Node.js - Rename file(s)
var glob = require('glob');
var fs = require('fs');
// Find file(s)
glob('fileName.ext', function(err, files) {
if (err) { throw err; }
// Check for file(s)
if (Boolean(files.length)) {
files.forEach(function(item, index, array) {
var newItem = item.replace('regexp', 'replacement');
if (item !== newItem) {
fs.rename(item, newItem, function(err) {
if (err) { throw err; }
console.log(item + ' renamed to ' + newItem);
});
} else {
console.log('Try the replace() method to rename file(s).');
}
});
} else {
console.log('File(s) not found.');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment