I still cannot grasp how to solve this. I tried solutions that were given in the How do I add a delay in a JavaScript loop? that supposedly has answers for my question but they have not worked for me.
Either I get the same thing that I tried (one wait function that runs once and that does not solve my problem). Or upon running the script, the script finishes with no output neither in the cli, nor in the Result.txt file that I want my results to be written into.
var path = require('path');
var fs = require('fs');
var VirusTotalApi = require("virustotal-api");
var virusTotal = new VirusTotalApi('<YOUR API KEY>');
fs.readdir('/home/username/Desktop/TEST/', function (err,files) {
if (err) {
return console.log('Unable to scan directory: ' +err);
}
files.forEach(function (file) {
var directoryPath = path.join('/home/username/Desktop/TEST/', file);
fs.readFile(directoryPath, (err, data) => {
if (err) {
console.log(`Cannot read file. ${err}`);
} else {
console.log(file);
virusTotal
.fileScan(data, `${file}`)
.then(response => {
let resource = response.resource;
virusTotal.fileReport(resource).then(result => {
fs.writeFile('Result.txt', `${file}: ` + JSON.stringify(result, ["verbose_msg","total","positives"]) + '\n', function (err) {
if (err) throw err;
if(!err) {
console.log(`${file}: Saved!`);
}
});
});
})
.catch(err => console.log(`Scan failed. ${err}`));
if(!err) {
console.log('Scan succesful');
}
}
});
});
});
Once more, I need a ForEach loop that would wait for 20 seconds, and then run the desired code. It would also need to wait before going on to the next for condition.