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


I would like to track new files in a Directory. I used the script given in the documentation:

var fs = require('fs');'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?


Problem courtesy of: htaidirt


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


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