Last active
April 10, 2023 23:30
-
-
Save scriptex/20536d8cda36221f91d69a6bd4a528b3 to your computer and use it in GitHub Desktop.
Rename all files in a folder with NodeJS
This file contains 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 { join } = require('path'); | |
const { readdirSync, renameSync } = require('fs'); | |
const [dir, search, replace] = process.argv.slice(2); | |
const match = RegExp(search, 'g'); | |
const files = readdirSync(dir); | |
files | |
.filter(file => file.match(match)) | |
.forEach(file => { | |
const filePath = join(dir, file); | |
const newFilePath = join(dir, file.replace(match, replace)); | |
renameSync(filePath, newFilePath); | |
}); | |
// Usage | |
// node rename.js path/to/directory 'string-to-search' 'string-to-replace' |
Amjad-ND
commented
Mar 3, 2021
@Amjad-ND what you're asking has nothing to do with the code shown in this gist.
I would suggest going to Stackoverflow or any similar website and try to find some resources/ideas which will help you accomplish your task.
Thank you man
perfect
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment