PhantomJS - copyTree



The copyTree method will copy a directory from one path to another. The first parameter is the source folder and second parameter is the destination folder. If the destination does not exist, it will be created and every file and folder from the source folder will be copied to the destination folder.

Folders will be recursively copied, if any of the file or folder fails while copying, it will throw an error – "Unable to copy directory tree SOURCE at DESTINATION" and execution will hang.

Syntax

Its syntax is as follows −

copyTree(source,destination);

Example

The following example shows the use of copyTree method.

var fs = require('fs'); 
var system = require('system'); 
var path1 = system.args[1]; 
var path2 = system.args[2]; 

console.log("Checking to see if source is a file:" + fs.isDirectory(path1)); 
console.log("Checking to see if destination is a file:" + fs.isDirectory(path2)); 
console.log("copying tree directory from source to destination"); 

fs.copyTree(path1, path2); 
console.log("Checking to see if destination is a file:" + fs.isDirectory(path2)); 

The above program generates the following output.

Command − phantomjs copytree.js newdirectory/a/b/c/file.txt destfolder

Checking to see if source is a file:true 
Checking to see if destination is a file:false 
copying tree directory from source to destination 
Checking to see if destination is a file:true
phantomjs_file_system_module_methods.htm
Advertisements