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
var Dircache = {};//Cache reduction Judge
function MakeDir (PATHSTR, callback) {
if (dircache[pathstr] = = 1) {
callback ();
} else {
fs.exists (pathstr, function (exists) {
if (exists = true) {
dircache[pathstr] = 1;
Callback ();
} else {
MakeDir (Path.dirname (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
var exec = require (' child_process '). exec;
EXEC (' xcopy d:\\work_new\\odinquest d:\\work_new\\newgame/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.
Fs.writefile (' Xcopy.bat ', cmdstr, function (err) {
if (err) throw err;
var exec = require (' child_process '). exec;
EXEC (' Call ' ' +process.cwd () + '/xcopy.bat ',
function (Error, stdout, stderr) {
if (error!== null) {
// Console.log (' EXEC error: ' + error ';}}});
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.