Home

Get File Metadata

The fs.Stats object contains a bunch of information about files.
The fs.Stats object can be found when running fs.stat(), fs.lstat(), fs.fstat(), and other synchronous version of those commands.
(This is part of a brief series on the fs module)

const { readdirSync, statSync } = require('fs');

const files = readdirSync('.');
const typeFromStat = (stat) => (stat.isDirectory() ? 'dir: ' : 'file: ');
files.forEach((name, idx) => {
  if (idx === 0) {
     const stat = statSync(name);
     const { atime, birthtime, ctime, mtime } = stat;
     const typeLabel = typeFromStat(stat);
     console.group(typeLabel, name);
     console.log({
       atime: atime.toLocaleString(),
       ctime: ctime.toLocaleString(),
       mtime: mtime.toLocaleString(),
       birthtime: birthtime.toLocaleString(),
     });
     console.groupEnd();
     console.log(); 
  }
  })

Details On A Few File Metadata Properties

atime: ACCESSED:

  • shows the last time a file was accessed.

ctime: STATUS CHANGED:

  • shows the last time the file status was changed

birthtime: CREATED:

  • shows the creation time.

mtime: MODIFIED:

  • shows the last time the file was modified

size: SIZE (in bytes):

  • shows the size of the file - may not be supported
Tags: