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 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 In the same folder, create a file called 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];
}];
node-findit
, simply use npm:npm install findit
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];
}];
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
- Load all the required Nodejs Packages using “require”.
- Get the path of the directory using path.join[] method.
- Pass the directory path and callback function in fs.readdir[path, callbackFunction] Method.
- The callback function should have error handling and result handling logic.
- inside callback function handle the error and run forEach on the array of files list from the directory.
- Apply your logic for each file or all the files inside the forEach function.
Full code:
first appeared on Web Development Blog StackFrame.