PhantomJS - copy



This method helps to copy a file from one location to another. It takes two parameters. The first parameter is the source file and the second parameter is the file path, where it has to be copied. It will throw an error if the source or destination file does not exist.

Syntax

Its syntax is as follows −

var  fs = require('fs');
fs.copy(sourcefile,destinationfile);

Example

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.isFile(path1)); 
console.log("Checking to see if destination is a file:" + fs.isFile(path2)); 
console.log("copying file from source to destination"); 
fs.copy(path1,path2); 

console.log("Checking to see if destination is a file:" + fs.isFile(path2)); 
console.log("contents from file are:"); 
console.log(fs.read(path2));

The above program generates the following output.

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.isFile(path1)); 
console.log("Checking to see if destination is a file:" + fs.isFile(path2)); 
console.log("copying file from source to destination"); 
fs.copy(path1,path2); 
console.log("Checking to see if destination is a file:" + fs.isFile(path2)); 
console.log("contents from file are:"); 
console.log(fs.read(path2)); 
phantomjs_file_system_module_methods.htm
Advertisements