Created
October 23, 2018 02:11
-
-
Save davidicus/52210c30cffea58483385bbb764da33b to your computer and use it in GitHub Desktop.
Rename files using regexp and Node.js
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 fs = require('fs'), | |
path = require('path'), | |
args = process.argv.slice(2), | |
dir = args[0], | |
match = RegExp(args[1], 'g'), | |
replace = args[2], | |
files; | |
files = fs.readdirSync(dir); | |
files.filter(function(file) { | |
return file.match(match); | |
}).forEach(function(file) { | |
var filePath = path.join(dir, file), | |
newFilePath = path.join(dir, file.replace(match, replace)); | |
fs.renameSync(filePath, newFilePath); | |
}); | |
// Usage | |
// node rename.js path/to/directory '\s' '-' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment