Nodejs invoke cmd command to implement replication directory

Source: Internet
Author: User
Tags button type parent directory require

This article describes how to call the Nodejs in the cmd command, so as to achieve the directory replication, very practical, there is a need for small partners to refer to.

Work has been required to some of the official website to copy, and replace some of the internal information, previously manual operation, or through their own write Firefox extensions to file operations.

Now the front end has Nodejs, why not Nodejs write a one-button type of it ~ ~

1. Copy Directory

When you copy a file, you will not succeed if you create a non-existent file directory. To be a parent directory exists. (Nodejs API contact time is not long, if there is a mistake, thank you).

This way when the file is written to detect whether the directory exists, does not exist to determine the parent directory, and then the first level of the directory to create back, then you can copy the file

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 var Dircache = {};//Cache reduction judge function MakeDir (Pathstr, callback) {if (dircache[pathstr] = = 1) {callback ();} else {Fs.ex Ists (Pathstr, function (exists) {if (exists = = True) {Dircache[pathstr] = = 1; callback ();} else {MakeDir ( PATHSTR), function () {Fs.mkdir (pathstr, function () {DIRCACHE[PATHSTR] = = 1; callback ();})}); } }) } };

2. Later still consider through the cmd command "xcopy" to achieve, but tried a good 9, direct implementation, but has not been, there are solutions, welcome to correct

?

1 2 3 4 5 6 7 8 var exec = require (' child_process '). exec; EXEC (' xcopy d:work_newodinquest d:work_newnewgame/s/e/q/y/i ', function (Error, stdout, stderr) {if (Error!== null)   {//console.log (' EXEC error: ' + error ');} });

Later, by writing the cmd command to the file, by calling the way, it is OK.

?

1 2 3 4 5 6 7 8 9 10 11 Fs.writefile (' Xcopy.bat ', cmdstr, function (err) {if (err) throw err; var exec = require (' child_process '). EXEC exec (' cal L "' +process.cwd () + '/xcopy.bat ', function (Error, stdout, stderr) {if (Error!== null) {//console.log (' EXEC error: ' + E RROR); }   }); });

Well, the code is written in that way, is a step-by-step asynchronous nesting, it is not put out, the province was jokes

The above mentioned is the entire content of this article, I hope you can enjoy.

Note < > : More Wonderful tutorials please focus on Triple programming

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.