Goal: Iterate through all files in X directory and getting the SHA256 value for each file.
The code below seems to almost work; it captures the SHA256 value for ONE file but failed at the next iteration. I've done some googling but due to my limited understanding of node I'm not able to find the answer.
Speculation: after the first iteration; the code is not able to find the full path of anymore?
Error: [Error: ENOENT: no such file or directory, open 'example.txt'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'example.txt'
}
example.txt
is the 2nd file in my directory, the code was able to get the value for the first file.
const fs = require('fs').promises
const hasha = require('hasha');
const path = require('path')
async function getAllFiles(pathToFiles){
let files = await fs.readdir(pathToFiles);
for (const file of files) {
const fullPath = path.join(pathToFiles, file)
const hash = await hasha.fromFile(file.toString(), {algorithm: 'sha256'});
console.log(hash);
}
}
getAllFiles('.').then(() => {
console.log("all done");
}).catch(err => {
console.log(err);
});