Node Copy file

Source: Internet
Author: User

I develop the process, often encountered, to copy the template to the current folder, often to go to the file, in order to save trouble, to solve this problem, wrote a node copy file.

//Flow Stream Pipeline pipe pipe//You can use data events if you want full control when reading streams and writing to the stream. However, for simple file replication, read and write streams can be piped to transmit data. varFS = require ("FS");varPath = require ("Path");/** Copy all files in directory including subdirectories * @src param{String} directories to be copied images or./images/* @dst param{String} copied to the specified directory example images Images/*/ //gets the current directory absolute path, where resolve () does not pass in ParametersvarFilePath =path.resolve ();varcopy =function (SRC,DST) {//To determine if the file takes time, you must synchronize    if(Fs.existssync (src)) {Fs.readdir (Src,function (err,files) {if(ERR) {Console.log (ERR);return;} Files.foreach (function (filename) {//url+ "/" +filename not available/direct connection, UNIX system is "/", Windows system is "\"                varURL =Path.join (src,filename), dest=Path.join (dst,filename);                    Console.log (URL);                Console.log (dest); Fs.stat (Path.join (Src,filename), function (err, stats) {if(ERR)Throwerr; //is a file                    if(Stats.isfile ()) {//Create read streamreadable =fs.createreadstream (URL); //Create write Streamwritable = Fs.createwritestream (dest,{encoding:"UTF8" }); //to transport a stream through a pipelinereadable.pipe (writable); //If the directory}Else if(Stats.isdirectory ()) {exists (URL, dest, copy);            }                });        });    }); }Else{Console.log ("The given directory does not exist, and the file is not read"); return; }}function exists (url,dest,callback) {fs.exists (dest,function (exists) {if(exists) {callback&&callback (Url,dest); }Else{            //second parameter directory permissions, default 0777 (read and Write permissions)Fs.mkdir (dest,0777, function (err) {if(ERR)Throwerr; Callback&&callback (Url,dest);        }); }    });} Exports.copy=copy;//copy ("./views/", "./www/");Copy"./.. /jdcase/homeappliances/", FilePath);

  

Node Copy file

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.