Skip to content

Instantly share code, notes, and snippets.

@nax3t
Last active November 11, 2019 19:30
Show Gist options
  • Save nax3t/01a07054e952b18cb4873a16d205d176 to your computer and use it in GitHub Desktop.
Save nax3t/01a07054e952b18cb4873a16d205d176 to your computer and use it in GitHub Desktop.
Reading, Deleting, and Renaming Files with Node JS File System (fs) Module
// FS docs: https://nodejs.org/api/fs.html
const fs = require('fs');
fs.readdir('.', (err, files) => {
if(err) console.log(err);
let counter = 0;
files.forEach(file => {
// // match and delete thumbnails
// if(file.match(/\d\dx\d\d/)
// || file.match(/\d\d\dx\d\d\d/)
// || file.match(/\d\d\d\dx\d\d\d/)) {
// file.unlink(file, err => {
// if(err) console.log(err);
// console.log('File deleted!')
// })
// }
// match and rename jpg's
if(file.match(/jpg$/)) {
counter++;
fs.rename(file, `${counter}.jpg`, err => {
if(err) console.log(err);
console.log('File renamed');
})
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment