How to list files and folders using nodejs?

Posted: February 12, 2013 by Sankar in Javascript
Tags: , , ,

list files and folders using nodejs

The below code may help you to list files and folders using nodejs.

var fs = require('fs');
fs.readdir('/', function (err, files) { // '/' denotes the root folder
  if (err) throw err;

   files.forEach( function (file) {
     fs.lstat('/'+file, function(err, stats) {
       if (!err && stats.isDirectory()) { //conditing for identifying folders
         $('ul#foldertree').append('<li class="folder">'+file+'</li>');
       }
       else{
        $('ul#foldertree').append('<li class="file">'+file+'</li>');
      }
     });
   });

});

Html section:

<ul id=”foldertree”></ul>

The code may help you when you create a folder browser or something like that using nodejs.

Reference links:
http://nodejs.org/api/fs.html
http://howtonode.org/do-it-fast

Have a nice day :lol: !

About these ads
Comments
  1. Deepak MP says:

    Really nice tutorial..

How's it? Your comments and suggestions...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s