最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I use FS to make a folder automatically? - Stack Overflow

programmeradmin3浏览0评论

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'

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'
Share Improve this question edited Jul 22, 2015 at 2:36 Bergi 665k161 gold badges1k silver badges1.5k bronze badges asked Jul 22, 2015 at 1:36 DavidDavid 1011 silver badge8 bronze badges 5
  • Shouldn't obj and i be declared as a global variable at the top rather than in the for loop. – tash Commented Jul 22, 2015 at 1:47
  • How did you check for errors? Did you set up a try/catch? Try doing try { fs.mkdirSync(path); } catch(e) { if ( e.code != 'EEXIST' ) throw e; } – aug Commented Jul 22, 2015 at 1:50
  • Okay, tried that. Its now telling me fs.js:796 return binding.mkdir(pathModule._makeLong(path), ^ Error: ENOENT: no such file or directory, mkdir '../audio/ – David Commented Jul 22, 2015 at 1:59
  • Seems like there is no /audio directory in which mkdir could create anything. – Bergi Commented Jul 22, 2015 at 2:37
  • You can use 'fs-extra' module instead, it has APIs to well meet your needs. – sayume Commented Jul 22, 2015 at 7:00
Add a ment  | 

3 Answers 3

Reset to default 10

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!')
});

NVM, found a solution. all I had to do was specify the location better.

if (!fs.existsSync('/home/user/recorder/audio/'+obj.key)){
    fs.mkdirSync('/home/user/recorder/audio/'+obj.key);
    console.log ('Folder '+obj.key+' has been created');

Just one line solution

!fs.existsSync(`./assets/`) && fs.mkdirSync(`./assets/`, { recursive: true })
发布评论

评论列表(0)

  1. 暂无评论