Skip to content

Instantly share code, notes, and snippets.

@capaj
Created July 5, 2013 06:32
Show Gist options
  • Save capaj/5932405 to your computer and use it in GitHub Desktop.
Save capaj/5932405 to your computer and use it in GitHub Desktop.
just listing files
var listFilesIn = function (name) {
var def = when.defer();
fs.readdir(basePath+name, function (err, files) {
if (err) {
def.reject(err);
}
var onlyfiles = [];
function checkIsFile(file, callback) {
// console.log("will check file: " + file);
fs.lstat(basePath + name + '/' + file, function(err, stats) {
if (!err && !stats.isDirectory()) { //condition for identifying folders
onlyfiles.push(file);
// console.log('Pushed file: '+file);
callback();
} else {
if(err){
console.warn("Error occured: " + err );
}
}
});
}
if (files) {
async.each(files, checkIsFile, function(err){
if (err) {
def.reject(err);
}
def.resolve(onlyfiles);
});
} else {
def.reject('No files found');
}
});
return def.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment