i am trying to access multiple files from the file system using node.js. As there is a lot of data which i am trying to process and the order in which they get processed does not matter, i want to do this in asynchronous fashion using callbacks. I have written the following code;
var fs = require('fs');
var basic_path="E:\\alphaDownload\\2016\\03";
var D1 = ['\\01','\\02','\\03','\\04'];
var D2 = ['\\00','\\01','\\02','\\03','\\04','\\05','\\06','\\07','\\08','\\09','\\10','\\11','\\12','\\13','\\14','\\15','\\16','\\17','\\18','\\19','\\20','\\21','\\22','\\23'];
var path=new Array(4);
for (var i=0;i<path.length;i++){
path[i]=new Array(24);
}
for (var j1=0; j1<D1.length; j1++){
for (var j2=0; j2<D2.length; j2++){
path[j1][j2]=basic_path+D1[j1]+D2[j2];
fs.readdir(path[j1][j2], function(err,items) {
console.log("=============="+path[j1][j2]+"================");
console.log(items);
if(err){throw err;}
else{for (var i=0; i<items.length; i++) {
fs.readFile(path[j1][j2]+'\\'+items[i],'utf8',function read(error, data){
if (error) {
console.log("-------**"+error);
}
else {
console.log(data);}
});
}}
});
}
}
but due to asynchronous nature of readdir
and readfile
functions value of indexing variables [j1] and [j2] change and a single path[3][23] is passed in the readfile
function.
Can anyone devise a wayout.