【问题标题】:Find recently created directory in node js在 node js 中查找最近创建的目录
【发布时间】:2019-11-11 20:33:51
【问题描述】:

我有一个路径中的文件夹列表。我想在 Node js 的路径中获取最近创建的文件夹/文件。

代码:

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

从我想要最近创建的文件列表中

【问题讨论】:

  • 与我们分享代码,到目前为止您已经尝试过什么。您可以通过查看文件夹的日期来获取最新创建的文件夹。
  • 如何获取日期和时间,只获取最新的文件
  • 看看这个stackoverflow.com/questions/15696218/…我希望你能从这里得到一些帮助。

标签: javascript node.js fs ctime


【解决方案1】:

您可以使用fs.statSync(path) 来检查文件更新时间。

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


【讨论】:

  • 每隔几秒就会创建文件,所以我想在目录中找到最后创建的文件
  • @yamuna 如果文件每隔几秒添加一次,你最好使用github.com/paulmillr/chokidar。您可以查看文件夹,它可以报告目录中的所有更改
【解决方案2】:
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 
});

【讨论】:

  • 我们可以获取文件ctime和mtime但是如何获取100个文件中最近的文件
  • 创建一个包含文件名和时间戳的对象数组,然后使用某种排序技术对时间进行排序,然后将其取出您应得的项目。
【解决方案3】:

fs 有一个stat 函数,它接受一个path 并返回一个包含最后访问、修改和创建的时间戳的stats 对象。

文档:https://nodejs.org/api/fs.html#fs_class_fs_stats

此处的用法示例:https://code-maven.com/system-information-about-a-file-or-directory-in-nodejs

您可以访问每个路径“stats”,将创建的时间戳添加到包含路径的数组中,然后按时间戳降序对数组进行排序。

类似:

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
})

【讨论】:

    【解决方案4】:

    您可以使用fs.stat(path/of/the/file)。 它返回以下信息:

    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
     }
    

    您应该检查值mtime,因为它代表“修改时间”或文件数据上次修改的时间。它由以下命令更改:mknod、utimes 和 write 系统调用。

    【讨论】:

      【解决方案5】:
      return _.max(files, function (f) {
        var fullpath = path.join(dir, f);
        return fs.statSync(fullpath).ctime;
      });
      

      上面的代码对我调用这个函数来获取最近的文件很有用

      【讨论】:

        猜你喜欢
        • 2011-07-27
        • 1970-01-01
        • 2023-04-09
        • 2012-11-22
        • 2022-01-09
        • 2014-07-21
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        相关资源
        最近更新 更多