Nodejs find file in folder

You can use OS help for this. Here is a cross-platform solution:

1. The bellow function uses ls and dir and does not search recursively but it has relative paths

var exec = require('child_process').exec;
function findFiles(folder,extension,cb){
    var command = "";
    if(/^win/.test(process.platform)){
        command = "dir /B "+folder+"\\*."+extension;
    }else{
        command = "ls -1 "+folder+"/*."+extension;
    }
    exec(command,function(err,stdout,stderr){
        if(err)
            return cb(err,null);
        //get rid of \r from windows
        stdout = stdout.replace(/\r/g,"");
        var files = stdout.split("\n");
        //remove last entry because it is empty
        files.splice(-1,1);
        cb(err,files);
    });
}

findFiles("folderName","html",function(err,files){
    console.log("files:",files);
})

2. The bellow function uses find and dir, searches recursively but on windows it has absolute paths

var exec = require('child_process').exec;
function findFiles(folder,extension,cb){
    var command = "";
    if(/^win/.test(process.platform)){
        command = "dir /B /s "+folder+"\\*."+extension;
    }else{
        command = 'find '+folder+' -name "*.'+extension+'"'
    }
    exec(command,function(err,stdout,stderr){
        if(err)
            return cb(err,null);
        //get rid of \r from windows
        stdout = stdout.replace(/\r/g,"");
        var files = stdout.split("\n");
        //remove last entry because it is empty
        files.splice(-1,1);
        cb(err,files);
    });
}

findFiles("folder","html",function(err,files){
    console.log("files:",files);
})

Suppose you want to list all the files in the current directory. One approach is to use the builtin fs.readdir method. This will get you an array of all the files and directories on the specified path:

fs = require('fs');

fs.readdir(process.cwd(), function (err, files) {
  if (err) {
    console.log(err);
    return;
  }
  console.log(files);
});

Unfortunately, if you want to do a recursive list of files, then things get much more complicated very quickly. To avoid all of this scary complexity, this is one of the places where a Node.js user-land library can save the day. Node-findit, by SubStack, is a helper module to make searching for files easier. It has interfaces to let you work with callbacks, events, or just plain old synchronously (not a good idea most of the time).

To install node-findit, simply use npm:

npm install findit

In the same folder, create a file called example.js, and then add this code. Run it with node example.js. This example uses the node-findit event-based interface.

//This sets up the file finder
var finder = require('findit').find(__dirname);

//This listens for directories found
finder.on('directory', function (dir) {
  console.log('Directory: ' + dir + '/');
});

//This listens for files found
finder.on('file', function (file) {
  console.log('File: ' + file);
});

Nodejs find file in folder

node.js fs

Do you need to get all files present in the directory or you want to scan a directory for files using node.js, then you’re on the correct web page because in this Nodejs How to Tutorial, we will learning How to get the list of all files in a directory in Node.js.

We will be using Node.js fs core module to get all files in the directory, we can use following fsmethods.

  • fs.readdir(path, callbackFunction) — This method will read all files in the directory.You need to pass directory path as the first argument and in the second argument, you can any callback function.
  • path.join() — This method of node.js path module, we will be using to get the path of the directory and This will join all given path segments together.

Steps to get list of all the files in a directory in Node.js

  1. Load all the required Nodejs Packages using “require”.
  2. Get the path of the directory using path.join() method.
  3. Pass the directory path and callback function in fs.readdir(path, callbackFunction) Method.
  4. The callback function should have error handling and result handling logic.
  5. inside callback function handle the error and run forEach on the array of files list from the directory.
  6. Apply your logic for each file or all the files inside the forEach function.

Full code:

first appeared on Web Development Blog StackFrame.

How do I find a file in node JS?

How to Find Files in Node..
First, you have to read the directory using readdir() method from the Node fs module..
Loop over the files returned from the readdir() method..
Find the extension of each file using path. ... .
You can find the file name with the path..

How do I read a folder in node JS?

The fs. readdir() method is used to asynchronously read the contents of a given directory. The callback of this method returns an array of all the file names in the directory.

What is __ Dirname Nodejs?

It gives the current working directory of the Node. js process. __dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.

What is the difference between Readdir and readdirSync?

readdir(). It is the same as fs. readdirSync(), but has the callback function as the second parameter.