How to list files and folders using nodejs?

Posted: February 12, 2013 by Sankar Vijayakumar 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 😆 !

Advertisements
Comments
  1. Deepak MP says:

    Really nice tutorial..

    Like

  2. Pyxouze says:

    I would like to list all the files I have stored on an external hard drive. Is it possible to list files and folders who are not in my node app folder ?

    Like

  3. I’m going to have to catch up, but it looks like I can use what you have to offer. Thanks.

    Liked by 1 person

  4. vyas says:

    unable to display in browser

    Like

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 )

Google+ photo

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

Connecting to %s