Use fs. mkdir in nodejs windows

Source: Internet
Author: User

Before that, you need to add a '.' In Front Of nodejs in windows '.'. This is strange. For example, '/Cache/t' cannot be directly written like this. It needs to be changed to './Cache/t '.

When mkdir is used in windows, a directory cannot be created at all. However, by comparing the example we wrote earlier, we found that when multiple paths exist, for example :'. /Cache/tt'. If there is no cache directory in the current directory, throw err will be executed in the function you write;

 
VaR FS = require ('fs'); FS. mkdir ('. /Cache/tt', 0776, function (ERR) {If (ERR) Throw err; // if no cache directory exists, an exception is thrown. The else {// my code // is executed when the cache directory exists }})

Through the analysis above YSS, we already know what to do below. Is to create a directory loop for each layer ~

See the following:Code:

/*** Nkdir for Windows (MD command) * @ Params is the same as the mkdir */function mkdirs (p, mode, callback) {callback = callback | function () {};/* First shoshould be. */If (P. charat (0 )! = '. ') {Callback (p); return;}/* Split it */var PS = P. split ('/'), Len = ps. length, I = 1, TMP; while (I ++ <Len) {TMP = ps. slice (0, I ). join ('/'); // console. log (TMP) // path. exists (TMP, function (exists) {// If (! Exists) {fs. mkdir (TMP, mode); // console. Log (I + ":" + TMP) //});} callback ();}

But when I write the above code, I find that it cannot be done through path. exists. If you remove the comment above. Output result:

 
E: \ code \ node> node e \ HTTP. JS. /cache. /Cache/PPT. /Cache/PPT/IMG. /Cache/PPT/img/imgloadlazy6 :. /Cache/PPT/img/imgloadlazy6 :. /Cache/PPT/img/imgloadlazy6 :. /Cache/PPT/img/imgloadlazy

It's strange ~

This issue needs to be found after YSS, I also hope that you can answer this 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.