find files by extension, *.html under a folder in nodejs

后端 未结 14 685
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 23:57

I\'d like to find all *.html files in src folder and all its sub folders using nodejs. What is the best way to do it?

var folder = \'/project1/src\';
var ex         


        
相关标签:
14条回答
  • 2020-12-08 00:37

    Can't add a comment because of reputation, but notice the following:

    Using fs.readdir or node-glob to find a wildcard set of files in a folder of 500,000 files took ~2s. Using exec with DIR took ~0.05s (non recursive) or ~0.45s (recursive). (I was looking for ~14 files matching my pattern in a single directory).

    So far, I have failed to find any nodejs implementation which uses low level OS wildcard searching for efficiency. But the above DIR/ls based code works wonderfully in windows in terms of efficiency. linux find, however, will likely be very slow for large directories.

    0 讨论(0)
  • 2020-12-08 00:42

    What, hang on?! ... Okay ya, maybe this makes more sense to someones else too.

    [nodejs 7 mind you]

    fs = import('fs');
    let dirCont = fs.readdirSync( dir );
    let files = dirCont.filter( function( elm ) {return elm.match(/.*\.(htm?html)/ig);});
    

    Do whatever with regex make it an argument you set in the function with a default etc.

    0 讨论(0)
提交回复
热议问题