Skip to content

Instantly share code, notes, and snippets.

@sh4hids
Created March 7, 2018 15:36
Show Gist options
  • Save sh4hids/9550dcad5316c1ab4f4acb0b2a8c8de0 to your computer and use it in GitHub Desktop.
Save sh4hids/9550dcad5316c1ab4f4acb0b2a8c8de0 to your computer and use it in GitHub Desktop.
Rename all the files in a directory with NodeJS
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