I have some code to copy files contained in an array for each source and destination directory in a dirs
array. Each iteration through the loop, it calls the function that copies. It looks like this:
var filesInEachDir ["file1", "file2", "file3"];
var dirs = [
{"source": "sourceDirectory1", "dest":"destinationDirectory1"},
{"source": "sourceDirectory2", "dest":"destinationDirectory2"},
{"source": "sourceDirectory3" "dest":"destinationDirectory3"},
];
for (var i = 0; i < dirs.length; i++){
fs.mkdir(dirs[i], function(err){
if(err){
console.log(err);
}else{
copyFiles(dirs[i], filesInEachDir);
}
});
}
function copyFiles(dirs, files){
for (var c = 0; c < files.length; c++){
fs.copy(files[c], dirs.source, dirs.dest, {replace: false}, function(err){
if (err){
console.log(err);
}else{
console.log('file copied');
}
});
}
}
For some reason, only the files in the last element of dirs
are copied. If I add another element to dirs
, its files are copied, and not any other. So it looks like i
is incrementing fully before calling the copyFiles
function. Why is this happening? How can I give copyFiles
each incrementing value of i
?