How do you get a list of the names of all files present in a directory in Node.js?

后端 未结 25 1697
天涯浪人
天涯浪人 2020-11-22 07:47

I\'m trying to get a list of the names of all the files present in a directory using Node.js. I want output that is an array of filenames. How can I do this?

25条回答
  •  野性不改
    2020-11-22 08:18

    function getFilesRecursiveSync(dir, fileList, optionalFilterFunction) {
        if (!fileList) {
            grunt.log.error("Variable 'fileList' is undefined or NULL.");
            return;
        }
        var files = fs.readdirSync(dir);
        for (var i in files) {
            if (!files.hasOwnProperty(i)) continue;
            var name = dir + '/' + files[i];
            if (fs.statSync(name).isDirectory()) {
                getFilesRecursiveSync(name, fileList, optionalFilterFunction);
            } else {
                if (optionalFilterFunction && optionalFilterFunction(name) !== true)
                    continue;
                fileList.push(name);
            }
        }
    }
    

提交回复
热议问题