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

后端 未结 14 859
隐瞒了意图╮
隐瞒了意图╮ 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:31

    I have looked at the above answers and have mixed together this version which works for me:

    function getFilesFromPath(path, extension) {
        let files = fs.readdirSync( path );
        return files.filter( file => file.match(new RegExp(`.*\.(${extension})`, 'ig')));
    }
    
    console.log(getFilesFromPath("./testdata", ".txt"));
    

    This test will return an array of filenames from the files found in the folder at the path ./testdata. Working on node version 8.11.3.

提交回复
热议问题