Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js: Track new files in a directory

Node.js: Track new files in a directory

Problem

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.

Problem courtesy of: htaidirt

Solution

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).

Solution courtesy of: robertklep

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js: Track new files in a directory

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×