0

A way to filter out those directories which need administration permission.

fs.readdirSync(currentPath, { withFileTypes: true }).forEach(element => {
   ....
})

Still shows all directories. Even those that need admin permission to be read/written to etc.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Bugger
  • 1
  • 1
    No, get the stats of each file individually. Or even better, just try to read them, then ignore them when you get a permission error - that's the only proper way to avoid race conditions, since permissions can change. – Bergi Feb 06 '23 at 15:59

1 Answers1

0

You can try to use the fs.access() method:

Tests a user's permissions for the file or directory specified by path

// Check if you have permission to read the file
function isFileReadable(file) {
  try {
    fs.accessSync(file, fs.constants.R_OK);
    return true;
  } catch (err) {
    return false;
  }
}

fs.readdirSync(currentPath, { withFileTypes: true }).forEach(element => {
    // If the file is readable, log it
    if(isFileReadable(element)) {
      console.log(`${element}`);
    }
   ...
})
Stanley Ulili
  • 702
  • 5
  • 7