Skip to content

Instantly share code, notes, and snippets.

@barhoring
Created February 15, 2020 05:46
Show Gist options
  • Select an option

  • Save barhoring/e4f139b95326f39e366e66f1f42dba09 to your computer and use it in GitHub Desktop.

Select an option

Save barhoring/e4f139b95326f39e366e66f1f42dba09 to your computer and use it in GitHub Desktop.
prints a directory contents sorted by modification date
const fs = require("fs");
const path = require("path");
const dirName = "./";
const dirPath = path.join(__dirname, dirName);
fs.readdir(dirPath, function(err, files) {
files = files
.map(function(fileName) {
return {
name: fileName,
time: fs.statSync(dir + "/" + fileName).mtime.getTime()
};
})
.sort(function(a, b) {
return -1 * (a.time - b.time);
})
.map(function(v) {
return v.name;
});
console.log(files);
});
@barhoring

barhoring commented Feb 15, 2020

Copy link
Copy Markdown
Author

This scripts prints the directory content it's in, sorted by modification date

node .\getDirectorySorted.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment