Last active
November 11, 2019 19:30
-
-
Save nax3t/01a07054e952b18cb4873a16d205d176 to your computer and use it in GitHub Desktop.
Reading, Deleting, and Renaming Files with Node JS File System (fs) Module
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
// 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