Nodejs invoke the cmd command to implement the replication directory _node.js

Source: Internet
Author: User
Tags button type parent directory

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.

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.