Skip to content

Instantly share code, notes, and snippets.

@victorsollozzo
Created November 23, 2012 09:44
Show Gist options
  • Save victorsollozzo/4134793 to your computer and use it in GitHub Desktop.
Save victorsollozzo/4134793 to your computer and use it in GitHub Desktop.
recursively find all files in a directory with given extension in node.js
var path = require('path')
var fs = require('fs')
function recFindByExt(base,ext,files,result)
{
files = files || fs.readdirSync(base)
result = result || []
files.forEach(
function (file) {
var newbase = path.join(base,file)
if ( fs.statSync(newbase).isDirectory() )
{
result = recFindByExt(newbase,ext,fs.readdirSync(newbase),result)
}
else
{
if ( file.substr(-1*(ext.length+1)) == '.' + ext )
{
result.push(newbase)
}
}
}
)
return result
}
ext_file_list = recFindByExt('/mypath','ext')
@skorunka
Copy link

Excellent, thank you.

@t-kabaya
Copy link

Umm....
Awesome!
Thank you!!!

@gandarez
Copy link

good one! thanks

@isharadilshan
Copy link

Superb !!

@soulehshaikh99
Copy link

soulehshaikh99 commented Jan 21, 2020

The above function generates this type of error:

Annotation 2020-01-21 002151

I made a workaround that uses a standard for loop instead of foreach loop, whenever it encounters an error of not able to read the directory it just continues the for loop leaving that directory. And this works fine for me.

Code:

const { join } = require('path');
const { readdirSync, statSync } = require('fs');

const getAllFiles = (dir, extn, files, result, regex) => {
    files = files || readdirSync(dir);
    result = result || [];
    regex = regex || new RegExp(`\\${extn}$`)

    for (let i = 0; i < files.length; i++) {
        let file = join(dir, files[i]);
        if (statSync(file).isDirectory()) {
            try {
                result = getAllFiles(file, extn, readdirSync(file), result, regex);
            } catch (error) {
                continue;
            }
        } else {
            if (regex.test(file)) {
                result.push(file);
            }
        }
    }
    return result;
}

const result = getAllFiles('C:\\Users\\Souleh', '.txt');
// console.log(result);
console.log(`Number of files found: ${result.length}`);

Output:

Annotation 2020-01-21 002347

@AkulG1
Copy link

AkulG1 commented Apr 9, 2020

How can I display them in frontend?

image

I got this by logging ext_file_list to console

@Abhishek-framewirk
Copy link

use a normalizeUrl npm module

Example:

const normalizeUrl = require('normalize-url');

var url = "public\\assets\\examples\\ind\\local-retail\\search\\search-and-view-item";
console.log(normalizeUrl(url));

Output: http://public/assets/examples/ind/local-retail/search/search-and-view-item

@hiulit
Copy link

hiulit commented Jun 15, 2021

I would also add !path.extname(file) just to make sure the function doesn't look inside a macOS .app, because it's actually a folder.

I have it like this:

const path = require('path')
const { readdirSync, statSync } = require('fs-extra')

const findFiles = (dir, ext, files, result, regex) => {
  files = files || readdirSync(dir)
  result = result || []
  regex = regex || new RegExp(`\\${ext}$`)

  for (let i = 0; i < files.length; i++) {
    let file = path.join(dir, files[i])
    if (statSync(file).isDirectory() && !path.extname(file)) {
      try {
        result = findFiles(file, ext, readdirSync(file), result, regex)
      } catch (error) {
        continue
      }
    } else {
      if (regex.test(file)) {
        result.push(file)
      }
    }
  }
  return result
}

@alopezari
Copy link

alopezari commented Nov 9, 2021

Just a recommendation, it would be safer and cleaner replacing the line 18:
if ( file.substr(-1*(ext.length+1)) == '.' + ext )

With the String method endsWith:
if (file.endsWith(featureExtension)) {

Anyway, great job, you inspired and helped a lot of people. Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment