Preliminary study Nodejs, amateur with node write a small script to automatically create directories and files, want to be helpful to those who need, if there are bugs or better optimization, please criticize and correct, thank you, the code is as follows:
1 varFS = require (' FS ');2 varTel = [3"1/2/4/6/a.tpl.html",4"1/2/4/7/9/c.tpl.html",5"1/3/5/8/b.tpl.html"];6 varDemo =function(Mulu) {7 varMuluatr = Mulu.split ("/");8Muluatr.foreach (function(value, i) {9i++;Ten if(i = =muluatr.length) { One if(!! Value.match (/\./g)) { A if(!!Fs.existssync (Mulu)) { - returnConsole.log ("File:" + Mulu + "already exists"); -}Else{ the if(Fs.opensync (Mulu, ' wx+ '))){ - returnConsole.log ("File:" + Mulu + "Create successful"); -}Else{ - returnConsole.log ("File:" + Mulu + "Create failed"); + } - } + } A } at varMunstr = Muluatr.slice (0, I). Join ("/"); - if(!!Fs.existssync (MUNSTR)) { - returnConsole.log ("directory:" + Munstr + "already exists"); -}Else{ - if(!Fs.mkdirsync (MUNSTR)) { - returnConsole.log ("directory:" + MUNSTR + "Create successful"); in}Else{ - returnConsole.log ("directory:" + munstr + "Create failed"); to } + } - }); the }; *Tel.foreach (function(value,i) {demo (value);});
As for the folders and files you want to create in an array form and assign to the Tel variable, the directory is relative to the directory where your script files are located.
Initial learning Nodejs, amateur use node to write a small script that automatically creates directories and files, hoping to help people in need