fs.readdir - optional filter

4,101 views
Skip to first unread message

Christian Joudrey

unread,
Mar 13, 2011, 3:55:25 PM3/13/11
to nodejs
Hey guys,

I don't know if this was brought up, but it would be interesting to be
able to filter the files that are returned when reading a directory.

Something along the lines of:

fs.readdir(path, callback)

and

fs.readdirSync(path, filter)

The filter could be either a String or an instance of RegExp.

This would allow us to easily filter files that are returned as such:

fs.readdir("/home/chris", function() { }, "*.txt")

or

fs.readdir("/home/chris", function() { }, /\.txt$/i)

Thoughts?

- Chris

Christian Joudrey

unread,
Mar 13, 2011, 3:56:42 PM3/13/11
to nodejs
Edit: I meant fs.readdir(path, [callback], [filter]) and
fs.readdirSync(path, [filter])

Isaac Schlueter

unread,
Mar 13, 2011, 5:29:30 PM3/13/11
to nod...@googlegroups.com
I wouldn't mind something like this, but:

a) the filter should go before the cb, not after. In node, the cb is
always the last arg.
b) the function shouldn't be called "readdir". The methods on the fs
module map directly to their "man 3" counterparts, so this function
maps pretty directly to the semantics of readdir(3).

--i

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

Ricardo Tomasi

unread,
Mar 14, 2011, 1:54:02 AM3/14/11
to nodejs
This can be done in userland without any hassle:

fs.readdirSync('/home/chris').filter(function(v){ return /
\.txt/.test(v); });

Christian Joudrey

unread,
Mar 14, 2011, 8:30:30 AM3/14/11
to nodejs
@Isaac Schlueter: I agree, in fact I think this should be a new
function, but didn't know what to call it.

@Ricardo: Of course you can, but if you can do that processing at the
time of reading the folders contents it would likely be faster. No
point in building up a big array of things that you won't use. I
understand your point tho.

Dominic Tarr

unread,
Mar 14, 2011, 10:22:57 AM3/14/11
to nod...@googlegroups.com
unless you are trying to ls directories with thousands of files thousands of times you probably have bigger problems someplace else.

what is your usecase?

Christian Joudrey

unread,
Mar 14, 2011, 11:36:57 AM3/14/11
to nodejs
To be honest, I am not dealing with thousands of files.

I just think providing fs.filterdir and fs.filterdirSync would
simplify the JS code (not having to .filter the return) and avoid
allocating the entries in the array for no reason.

We could even add a second option for filtering whether only dirs or
only files should be returned.

fs.filterdir(dir, filter, [type = FILTER_TYPE_FILES |
FILTER_TYPE_DIRECTORY], [callback])

and

fs.filterdirSync(dir, filter, [type = FILTER_TYPE_FILES |
FILTER_TYPE_DIRECTORY])

This would easily allow something like this:

fs.filterdir('/tmp', /*\.txt$/i, FILTER_TYPE_FILES, function(err,
files) {

});

Thoughts?

On Mar 14, 10:22 am, Dominic Tarr <dominic.t...@gmail.com> wrote:
> unless you are trying to ls directories with thousands of files thousands of
> times you probably have bigger problems someplace else.
>
> what is your usecase?
>

Isaac Schlueter

unread,
Mar 14, 2011, 1:42:59 PM3/14/11
to nod...@googlegroups.com
Actually, adding a "filtertype" flag that could be either directories
or files would make readdir a lot slower, since it'd have to stat
everything in the directory. Then there's the question of whether it
should use stat or lstat (ie, is a symlink to a directory interpreted
as a directory or a file?)

The more I think about this, I really think this belongs outside of
node core. There are a lot of edge cases to consider.

Christian Joudrey

unread,
Mar 14, 2011, 4:15:16 PM3/14/11
to nodejs
Yeah I don't think it should be in `readdir` either, which is why I
suggested `fs.filterdir`.

The idea was to offer a built-in way to easily filter through a
directory's contents like in Ruby: Dir["*.txt"].

I suppose I will code it up in JS for the moment instead.

Pau

unread,
Mar 14, 2011, 4:19:14 PM3/14/11
to nod...@googlegroups.com
+1 to keep this in "userland"

ls | grep '.txt'

Isaac Schlueter

unread,
Mar 14, 2011, 5:03:11 PM3/14/11
to nod...@googlegroups.com
Dir["*.txt"] can be done pretty easily today.

npm install glob
require("glob").glob("*.txt", function (er, files) { ... })
or:
files = require("glob").globSync("*.txt")

Christian Joudrey

unread,
Mar 14, 2011, 10:30:06 PM3/14/11
to nodejs
Wow!

Well to that I say:

One day, in <Node.js Mailing List>:
[3:55pm] Christian Joudrey: it would be good to have a file system
Glob functionality (to get an array of filenames)

Thanks a bunch!
Reply all
Reply to author
Forward
0 new messages