I'm trying to use code to read an array and create folders using the name as one of the parameters if that name doesn't exist. I've been using fs to make a simple loop, like so
var streamsRepository = streamsRepositoryFactory(__dirname + '/streams.json');
var obj = streamsRepository.streams[i];
var i;
for(i = 0; i < streamsRepository.streams.length; i++) {
var obj = streamsRepository.streams[i];
console.log('Folder '+obj.key+' is Created');
if (!fs.existsSync('../audio/'+obj.key)){
fs.mkdirSync('../audio/'+obj.key);
}
}
But every time I keep getting the message.
Folder AAAA is Created
fs.js:796
return binding.mkdir(pathModule._makeLong(path),
^
Error: ENOENT: no such file or directory, mkdir '../audio/AAAA'
Create a new folderUse fs. mkdir() or fs. mkdirSync() or fsPromises. mkdir() to create a new folder.
Creating Permanent Directories with fs. mkdir Family of Functions. To create permanent directories, we can use the mkdir function to create them asynchronously.
The best solution would be to use the npm module called node-fs-extra. It has a method called mkdir which creates the directory you mentioned. If you give a long directory path, it will create the parent folders automatically.
mkdir() Method - GeeksforGeeks.
Have a test below
'use strict';
var fs = require('fs');
// fs.mkdirSync('folda'); // success
fs.mkdirSync('/parent-not-exists/folda');
// Failed,if parent folder isn't exists,will throw
// Error: ENOENT, no such file or directory '/parent-not-exists/folda'
solution
use mkdirp,Recursively mkdir, like mkdir -p
var mkdirp = require('mkdirp');
mkdirp('/tmp/foo/bar/baz', function (err) {
if (err) console.error(err)
else console.log('pow!')
});
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