PHP的にglobコマンドでファイル一覧を取得したかったので書いてみました。
var
fs = require('fs');
fs.glob = function (path, pattern, callback) {
fs.readdir(path, function (cb) {
return function (err, files) {
if (!err) {
var files2 = [];
for (var index in files) {
if (files[index].match(pattern)) {
files2.push(files[index]);
}
}
cb(err, files2);
}
else {
cb(err, files);
}
}
} (callback));
};
fs.glob('.', /.*\.log/i, function (err, files) {
console.log(files);
});
こんなんでいいのかな。一応動いているっぽいです。
[ 'npm-debug.log', 'service.log', 'test.20120621.log', 'test.20120621090035.log', 'test.20120621090058.log', 'test1.log', 'test1.old.log', 'test2.log' ]
タグ: JavaScript, node.js

HOMMA Teppei

