Also to explain why "ls" is slow - it has to do just that - stat each entry and then sort (plus you have the overhead of a separate process of course).
On Thu, Jun 21, 2012 at 4:47 PM, Tim Caswell <t...@creationix.com> wrote: > What you want to do is readdir, and then stat each entry. Once you have > the file stats, you can sort the list in JavaScript. The control-flow can > be tricky the first time you do this since stat is an async function. > There are many examples on the web of readdir with stat. There might even > be libraries to do this in npm. > > > On Thu, Jun 21, 2012 at 2:51 PM, Adeel Qureshi <adeelmahm...@gmail.com>wrote: > >> I was trying to get a listing of files in a directory and send the output >> to clients using socket.io. First I tried with readdir which works good >> and pretty fast but is not able to sort the files by modified date (or >> atleast I wasnt able to find out how). >> >> fs.readdir(logDrivesPath.replace(/#/,drive), function(err, files){ >> console.log(files); >> socket.emit('filesList', files); >> }); >> >> so i then used child process ls to get the listing in correct sorting >> order >> >> var ls = exec('ls -t ' + logDrivesPath.replace(/#/,drive) + ' | xargs -n1 >> basename', >> function(err, stdout, stderr){ >> console.log("ls exec completed"); >> if(err == null){ >> socket.emit('filesList', >> stdout.toString('utf8').split('\n')); >> } >> else{ >> console.log(stderr); >> console.log(err); >> } >> }); >> >> the second option works fine but its much slower than the readdir option. >> It almost takes 3-4 seconds to get the listing while readdir returns the >> same list in less than a second so there is a significant difference. I was >> wondering if there is a way to use readdir but get the files sorted >> properly and secondly why is the childprocess option so much slower than >> readdir. >> >> Thanks >> Adeel >> >> -- >> Job Board: http://jobs.nodejs.org/ >> Posting guidelines: >> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >> You received this message because you are subscribed to the Google >> Groups "nodejs" group. >> To post to this group, send email to nodejs@googlegroups.com >> To unsubscribe from this group, send email to >> nodejs+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/nodejs?hl=en?hl=en >> > > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to nodejs@googlegroups.com > To unsubscribe from this group, send email to > nodejs+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en