Created
March 7, 2018 15:36
-
-
Save sh4hids/9550dcad5316c1ab4f4acb0b2a8c8de0 to your computer and use it in GitHub Desktop.
Rename all the files in a directory 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 dirPath = path.join(__dirname, './dir/relative/path'); | |
fs.readdir(dirPath, (err, files) => { | |
let x = 1; | |
files.forEach((file) => { | |
let oldPath = path.join(dirPath,file); | |
let newPath = path.join(dirPath,`filename${x}.extension`); | |
fs.renameSync(oldPath, newPath, (err) => { | |
if (err) { | |
console.log(err); | |
} | |
}); | |
x++; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment