[英]Get all files with specified extension node.js
I am using node.js. 我正在使用node.js。 I want to loop through all files with extension .coffee, but I have nowhere found an example.
我想遍历所有扩展名为.coffee的文件,但是我无处找不到示例。
Following function will return all the files in the specified directory with the regex provided. 以下函数将返回提供的正则表达式在指定目录中的所有文件。
Function 功能
var path = require('path'), fs=require('fs');
function fromDir(startPath,filter,callback){
//console.log('Starting from dir '+startPath+'/');
if (!fs.existsSync(startPath)){
console.log("no dir ",startPath);
return;
}
var files=fs.readdirSync(startPath);
for(var i=0;i<files.length;i++){
var filename=path.join(startPath,files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()){
fromDir(filename,filter,callback); //recurse
}
else if (filter.test(filename)) callback(filename);
};
};
Usage 用法
fromDir('../LiteScript',/\.coffee$/,function(filename){
console.log('-- found: ',filename);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.