jamuna
jamuna

Reputation: 91

Find recently created directory in node js

I have a list of folders in a path. I want to get the most recently created folders/files in the path in Node js.

code :

fs.readdir(path,function(err,files){
console.log("files in the path",files);
}

from the list of files i want the most recently created

Upvotes: 0

Views: 1797

Answers (5)

jamuna
jamuna

Reputation: 91

return _.max(files, function (f) {
  var fullpath = path.join(dir, f);
  return fs.statSync(fullpath).ctime;
});

The above code worked for me calling this function to get the recent file

Upvotes: 0

Wolfetto
Wolfetto

Reputation: 1130

You can use fs.stat(path/of/the/file). It returns the following information:

Stats {
  dev: 2114,
  ino: 48064969,
  mode: 33188,
  nlink: 1,
  uid: 85,
  gid: 100,
  rdev: 0,
  size: 527,
  blksize: 4096,
  blocks: 8,
  atimeMs: 1318289051000.1,
  mtimeMs: 1318289051000.1,
  ctimeMs: 1318289051000.1,
  birthtimeMs: 1318289051000.1,
  atime: Mon, 10 Oct 2011 23:24:11 GMT,
  mtime: Mon, 10 Oct 2011 23:24:11 GMT,
  ctime: Mon, 10 Oct 2011 23:24:11 GMT,
  birthtime: Mon, 10 Oct 2011 23:24:11 GMT
 }

You should check the value mtime because it represents the "Modified Time" or Time when file data last modified. It is changed by the following commands: mknod, utimes and write system calls.

Upvotes: 0

cam
cam

Reputation: 3616

fs has a stat function which takes a path and returns a stats object contains the last accessed, modified and created timestamps.

Documentation: https://nodejs.org/api/fs.html#fs_class_fs_stats

Example usage here: https://code-maven.com/system-information-about-a-file-or-directory-in-nodejs

You could access each paths 'stats', add the created timestamps into an array with the path and then sort the array by timestamp descending.

Something like:

const paths = [
  '/path/to/file1',
  '/path/to/file2',
  '/path/to/file3',
  '/path/to/file4',
]

let pathTimestamps = []

// Add path timestamps to the pathTimestamps array as an object containing the path and created timestamp
paths.forEach(path => {
  fs.stat(path, (err, stats) => {
    pathTimestamps.push({ path: path, createdTimestamp: stats.birthtimeMs })
  }
})

// Sort the array by timestamp descending (newest paths first)
pathTimestamps.sort(a, b => {
  b.createdTimestamp - a.createdTimestamp
})

Upvotes: 0

Amit
Amit

Reputation: 4290

You can use fs.statSync(path) to check for file updatetime.

fs.readdir(path,function(err,files){
 files.filter(file => {
const stats = fs.statSync(path)
  return someSpecificDate < stats.mtime
});
})


Upvotes: 2

Mr.Throg
Mr.Throg

Reputation: 1005

const fs = require('fs');

var directory = './';

fs.readdir(directory, (err, files) => {
    if(err) {
        // handle error; e.g., folder didn't exist
    }
    // files refer to array of files in directory iterate through every file and refer the below link for file info operation

      https://code-maven.com/system-information-about-a-file-or-directory-in-nodejs
    for getting file/dir information 
});

Upvotes: 0

Related Questions