-
-
Save jdomzhang/a201d9a023f3f4575a2018eb7b13fc52 to your computer and use it in GitHub Desktop.
Rename all files in a folder with NodeJS
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
const fs = require('fs'); | |
const path = require('path'); | |
const args = process.argv.slice(2); | |
const dir = args[0]; | |
const match = RegExp(args[1], 'g'); | |
const replace = args[2]; | |
const files = fs.readdirSync(dir); | |
files | |
.filter(file => { | |
return file.match(match); | |
}) | |
.forEach(file => { | |
const filePath = path.join(dir, file); | |
const newFilePath = path.join(dir, file.replace(match, replace)); | |
fs.renameSync(filePath, newFilePath); | |
}); | |
// Usage | |
// node rename.js path/to/directory 'string-to-search' 'string-to-replace' | |
// on windows: | |
// node .\rename.js .\banner banner- \ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment