Nodejs readdir - only find files
Asked Answered
F

4

7

When reading a directory, I currently have this:

  fs.readdir(tests, (err, items) => {

      if(err){
        return cb(err);
      }

      const cmd = items.filter(v => fs.lstatSync(tests + '/' + v).isFile());

      k.stdin.end(`${cmd}`);
  });

first of all I need a try/catch in there around fs.lstatSync, which I don't want to add. But is there a way to use fs.readdir to only find files?

Something like:

fs.readdir(tests, {type:'f'}, (err, items) => {});

does anyone know how?

Freewill answered 13/7, 2018 at 21:42 Comment(0)
S
1

Unfortunately, fs.readdir doesn't have an option to specify that you're only looking for files, not folders/directories (per docs). Filtering the results from fs.readdir to knock out the directories is your best bet.

https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_fs_readdir_path_options_callback

The optional options argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use for the filenames passed to the callback. If the encoding is set to 'buffer', the filenames returned will be passed as Buffer objects.

Stillness answered 13/7, 2018 at 21:56 Comment(0)
W
9

Starting from node v10.10.0, you can add withFileTypes as options parameter to get fs.Dirent instead of string.

// or readdir to get a promise
const subPaths = fs.readdirSync(YOUR_BASE_PATH, { 
  withFileTypes: true
});
// subPaths is fs.Dirent[] type
const directories = subPaths.filter((dirent) => dirent.isFile());
// directories is string[] type

more info is located at node documentation:

Wimbush answered 22/8, 2021 at 7:12 Comment(0)
S
1

Unfortunately, fs.readdir doesn't have an option to specify that you're only looking for files, not folders/directories (per docs). Filtering the results from fs.readdir to knock out the directories is your best bet.

https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_fs_readdir_path_options_callback

The optional options argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use for the filenames passed to the callback. If the encoding is set to 'buffer', the filenames returned will be passed as Buffer objects.

Stillness answered 13/7, 2018 at 21:56 Comment(0)
F
1

Yeah fs.readdir can't do this currently (only read files or only read dirs).

I filed an issue with Node.js and looks like it may be a good feature to add.

https://github.com/nodejs/node/issues/21804

Freewill answered 14/7, 2018 at 4:59 Comment(0)
C
0

If your use case is scripting/automation. You might try fs-jetpack library. That can find files in folder for you, but also can be configured for much more sophisticated searches.

const jetpack = require("fs-jetpack");

// Find all files in my_folder
const filesInFolder = jetpack.find("my_folder", { recursive: false }));
console.log(filesInFolder);

// Example of more sophisticated search:
// Find all `.js` files in the folder tree, with modify date newer than 2020-05-01
const borderDate = new Date("2020-05-01")
const found = jetpack.find("foo", {
  matching: "*.js",
  filter: (file) => {
    return file.modifyTime > borderDate
  }
});
console.log(found);
Crossstitch answered 30/11, 2021 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.