Created
September 19, 2021 20:34
-
-
Save robinmollah/7c6cd1d1f6b329d6a61e202195a945ed to your computer and use it in GitHub Desktop.
Sort files by last modified date
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 fs = require("fs"); | |
function lastModifiedFiles(dir) { | |
fs.readdir(dir, (err, files) => { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
console.log(files); | |
let tmpFiles = files | |
.map((file) => { | |
return { | |
name: file, | |
time: fs.statSync(dir + file).mtime, | |
}; | |
}) | |
.sort((a, b) => { | |
return b.time - a.time; | |
}); | |
console.log(tmpFiles); | |
}); | |
} | |
lastModifiedFiles("./"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment