Last active
August 29, 2015 14:25
-
-
Save leotm/1980d616da01381c7cdf to your computer and use it in GitHub Desktop.
Node.js - Rename file(s)
This file contains hidden or 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
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