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