javascript - Get file name from stats object -


i use fs.stat getting information file, want file name stats object, did searching , didnt find help.

i explain code now, maybe find solution, code:

function index(response, lang) { temp.loadtpl('templates/' + lang + '/content.html', function (content) {     fs.readdir('files/', function (err, files) {         temp.loadtpl('templates/' + lang + '/files.html', function (data) {             if (err) console.log(err);             var i, fnames, len = files.length, filesnameshtml = '';             fnames = files;             (i = 0; < len; i++) {                 fs.stat('files/' + files[i] , function (err, stats) {                     console.log(i);                     if (stats.isfile()) temp.write('type', 'file-o');                     else temp.write('type', 'folder');                     temp.write('fname', fnames[i]);                     filesnameshtml += temp.transtpl(data);                 });             }             settimeout(function () {                 temp.write('files', filesnameshtml);                 response.write(temp.transtpl(content));             }, 100);         });     }); }); } 

the relevant part is: use fs.readdir read directiony files, , run on result loop, , every file run stat function that:

for (i = 0; < len; i++) {    fs.stat('files/' + files[i] , function (err, stats) {       temp.write('fname', fnames[i]);     }); } 

the problem in this: fnames[i], until callback invoked, i variable changed, , same result every file.

maybe have solution how file name inside stat callback function.

the problem happend because try use async functions...

edit: thought on solution, wants...

you can put fs.stat function function , pass function file name, , use inside stat callback function.

your edit should solve problem of variable scoping. haven't explained well, know mean.

you've got for loop, synchronous, you're using start asynchronous functions. you're waiting until files have been fs.stated before doing final part, assuming fs.stat won't take more 100ms. asynchronous things go, fs.stat isn't slow, 100ms should enough time unless have large number of files. however, when you're doing things slower and/or take variable length of time, you'll want control timing more tightly last bit after it's finished last of async bits.

there npm modules kind of management of asynchronous functions. i'm going answer using async module, because that's 1 know, there equally valid ways of doing other modules using promises.

i'd this:

var async = require('async');  function index(response, lang) {     temp.loadtpl('templates/' + lang + '/content.html', function (content) {         fs.readdir('files/', function (err, files) {             temp.loadtpl('templates/' + lang + '/files.html', function (data) {                 if (err) console.log(err);                 var i, fnames, len = files.length, filesnameshtml = '';                 fnames = files;                 async.each(files, function(file, done) {                     fs.stat('files/' + file , function (err, stats) {                         console.log(i);                         if (stats.isfile()) temp.write('type', 'file-o');                         else temp.write('type', 'folder');                         temp.write('fname', fnames[i]);                         filesnameshtml += temp.transtpl(data);                         done(null);                     });                 }, function() {                     temp.write('files', filesnameshtml);                     response.write(temp.transtpl(content));                 });             });         });     }); } 

(then npm install async .)

there other things change, tricky stuff first time see i'll refrain. suffice it's worth exploring async module or equivalent if want write javascript code involving multiple calls asynchronous functions.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -