htaidirt htaidirt - 21 days ago 9
Node.js Question

Node.js: Track new files in a directory

I would like to track new files in a directory. I used the script given in the documentation: http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener

var fs = require('fs');
fs.watch('mydir/', function (event, filename) {
console.log('event is: ' + event);
if (filename) {
console.log('filename provided: ' + filename);
} else {
console.log('filename not provided');
}
});


And I added files to
mydir/
using
touch hello.txt
for example.

When running the script, I don't get the new file name, because the emitted event is
rename
!! Here is the console output.

event is: rename
filename not provided


How can I get the file new name
hello.txt
?

Thanks.

Answer

The same page also describes your issue:

Providing filename argument in the callback is not supported on every platform (currently it's only supported on Linux and Windows). Even on supported platforms filename is not always guaranteed to be provided.

I think your best bet is to switch to another module providing similar functionality, like watch (which I don't have any experience with myself).

Comments