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