node. JS folder directory structure creation

Source: Internet
Author: User

The first contact with Nodejs's file system was stunned by its asynchronous response, and later found Nodejs to determine if the folder exists and create a folder is also a synchronous method, but still want to try to use asynchronous method to implement.

The method used:
Fs.exists(path, callback);
Fs.mkdir(path, [mode], callback);

The implementation of the creation code for the folder directory structure is as follows:

//Create a folderfunctionmkdir (POS, dirarray,_callback) {varLen =dirarray.length;    Console.log (len); if(pos >= len | | pos > 10) {_callback (); return; }    varCurrentdir = ' ';  for(vari= 0; I <=pos; i++){        if(i!=0) currentdir+= '/'; Currentdir+=Dirarray[i]; } fs.exists (Currentdir,function(exists) {if(!exists) {Fs.mkdir (Currentdir,function(err) {if(Err) {Console.log (' Error creating folder! ‘); }Else{console.log (Currentdir+ ' folder-Create success! ‘); mkdir (POS+1, Dirarray,_callback);        }            }); }Else{console.log (Currentdir+ ' folder-already exists! ‘); mkdir (POS+1, Dirarray,_callback); }    });}//Create a directory structurefunctionmkdirs (dirpath,_callback) {varDirarray = Dirpath.split ('/')); Fs.exists (Dirpath,function(exists) {if(!exists) {mkdir (0, Dirarray,function() {Console.log (' Folder creation finished! Ready to write to file! ');            _callback ();        }); }Else{Console.log (' Folder already exists! Ready to write to file! ');        _callback (); }    });}

First, you need to create a directory structure into an array, and then the main use of deep-search ideas to achieve (depth of the length of the array).

The reference documentation is as follows:
http://www.cnblogs.com/gaojun/p/4159488.html#_Toc406152896

node. JS folder directory structure creation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.