Created
July 30, 2018 19:11
-
-
Save pianosnake/44ab1174228905fe25b7d3e23b7f48b0 to your computer and use it in GitHub Desktop.
Get the name of the most recently created file in a given directory using Node.js
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'); | |
function getMostRecentFile(dir) { | |
const files = fs.readdirSync(dir); | |
let earliestCreate = 0; | |
let mostRecentFile = files[0]; | |
files.forEach(file => { | |
if (fs.statSync(path.join(dir, file)).ctime > earliestCreate) { | |
mostRecentFile = file; | |
} | |
}); | |
return mostRecentFile; | |
} | |
//getMostRecentFile('./my-dir'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment