Node.js the method of file copy and directory traversal of local file operation _node.js

Source: Internet
Author: User

File copy
Nodejs provides a basic file manipulation API, but the advanced functionality like file copy is not available, so we first practicing the file copy program. Like the copy command, our program needs to be able to accept two parameters for the source file path and the destination file path.

Small file copy
we use the Nodejs built-in FS module to simply implement this program as follows.

var fs = require (' FS ');

function copy (src, DST) {
  Fs.writefilesync (DST, Fs.readfilesync (SRC));
}

function Main (argv) {
  copy (argv[0], argv[1]);

Main (Process.argv.slice (2));

The above program uses Fs.readfilesync to read the contents of the file from the source path and writes the contents of the file to the target path using Fs.writefilesync.

Bean Knowledge: The process is a global variable that can obtain command-line arguments through PROCESS.ARGV. Because argv[0] is fixed equal to the absolute path of the Nodejs executor, argv[1] is equal to the absolute path of the main module, so the first command-line argument starts at the location of Argv[2.

Large file copy
the top of the program to copy a few small files is not a problem, but this one-time to read all the contents of the file into memory and then write to the disk in a single way is not suitable for copying large files, memory will burst the warehouse. For large files, we can only read a little bit of writing until the copy is complete. So the above procedure needs to be reformed as follows.

var fs = require (' FS ');

function copy (src, DST) {
  fs.createreadstream (src). Pipe (Fs.createwritestream (DST));

function Main (argv) {
  copy (argv[0], argv[1]);

Main (Process.argv.slice (2));

The above program uses Fs.createreadstream to create a read-only stream of source files, and uses Fs.createwritestream to create a write-only stream of the target file, and to connect two streams of data with the pipe method. When something is connected, it is said to be abstract, and the water runs from one barrel to the other.

Traverse Directory

Traversing a directory is a common requirement for manipulating files. For example, to write a program, you need to find and process all the JS files in the specified directory, you need to traverse the entire directory.

Recursive algorithm
The recursive algorithm is commonly used when traversing directories, otherwise it is difficult to write concise code. Recursive algorithms are similar to mathematical induction and solve problems by shrinking the scale of the problem. The following example illustrates this approach.

function factorial (n) {
  if (n = = 1) {return
    1;
  } else {return
    n * factorial (n-1);}
}

The function above is used to compute the factorial of N (n!). As you can see, when n is greater than 1 o'clock, the problem is reduced to the factorial that calculates n times N-1. When N equals 1 o'clock, the problem reaches the minimum scale and does not need to be simplified so that it returns 1 directly.

Traps: Code written using recursive algorithms is concise, but because of a function call once at a time, the recursive algorithm needs to be converted to a cyclic algorithm to reduce the number of function calls when the performance needs to be given priority.

Traversal algorithm
The directory is a tree structure, in the traversal generally use depth first + First order traversal algorithm. Depth first, meaning that once you reach a node, you go through the child nodes instead of the neighbor nodes. First-order traversal means that the first time a node is reached, even if the traversal completes, rather than returning to a node for the last time. Therefore, when using this traversal method, the traversal order of the tree below is a > B > D > E > C > F.

     A
     /\
    B  C
    /\
   D  E  F

Synchronous traversal
after understanding the necessary algorithms, we can simply implement the following directory traversal functions.

function Travel (dir, callback) {
  Fs.readdirsync (dir). ForEach (function (file) {
    var pathname = Path.join (dir, file);

    if (Fs.statsync (pathname). Isdirectory ()) {
      Travel (pathname, callback);
    } else {
      callback (pathname);
    }
  });
}

As you can see, the function takes a directory as the starting point for the traversal. When a subdirectory is encountered, it goes through the subdirectory first. When a file is encountered, the absolute path to the file is passed to the callback function. After the callback function gets the file path, it can make various judgments and processing. So suppose you have the following directory:

-/home/user/
  -foo/
    x.js
  -bar/
    y.js
  z.css

When you traverse the directory using the following code, the input is as follows.

Travel ('/home/user ', function (pathname) {
  console.log (pathname);
});

/home/user/foo/x.js
/home/user/bar/y.js
/home/user/z.css

Asynchronous traversal
If the asynchronous API is used to read the directory or read the state of the file, directory traversal functions can be complex to implement, but the principle is identical. The asynchronous version of the travel function is as follows.

function Travel (dir, callback, finish) {
  fs.readdir (dir, function (err, files) {
    (function next (i) {
      if (i) ; Files.length) {
        var pathname = Path.join (dir, files[i]);

        Fs.stat (pathname, function (err, stats) {
          if (stats.isdirectory ()) {
            Travel (pathname, callback, function () {
   next (i + 1);
            });
          else {
            callback (pathname, function () {
              Next (i + 1);});}}
        );
      else {
        Finish & & Finish ();
      }
    (0));
  }


The writing techniques for asynchronous traversal functions are not described in detail here and will be covered in detail in the following chapters. In short, we can see that asynchronous programming is still quite complicated.

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.