I'm trying to list the files in the S:/test folder, which is in my network (it's not a local directory). I was wondering how to do this? The code so far looks like this:
const testFolder = 's:/test';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
})
I've tried changing the path to S:test, s:\test to no avail, the error is always "Cannot read 'forEach' of undefined"
If this is windows (which I assume it is), then you need to do a couple things:
fs.readdir()
callback so if there is an error, you can see exactly what the error is.Working code:
const fs = require('fs');
const testFolder = 's:\\test';
fs.readdir(testFolder, (err, files) => {
if (err) return console.log(err);
files.forEach(file => {
console.log(file);
});
});
I just tried this code on my own hard drive and it works just fine.
And, FYI I pretty much always use ES6 for/of
now in modern node.js rather than .forEach()
because it's much more efficient for the interpreter and it gives you more loop control (for example, you can use break
to exit the loop).
const testFolder = 's:\\test';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
if (err) return console.log(err);
for (let file of files) {
console.log(file);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With