PhantomJS - copyTree



copyTree 方法会将一个目录从一个路径复制到另一个路径。第一个参数是文件夹,第二个参数是目标文件夹。如果目标文件夹不存在,它会创建目标文件夹,并且源文件夹中的每个文件和文件夹都会复制到目标文件夹中。

文件夹会递归复制,如果在复制过程中有任何文件或文件夹失败,它将抛出错误 – “无法在 DESTINATION复制目录树 SOURCE”并且执行会暂停。

语法

它的语法如下 −

copyTree(source,destination);

示例

以下示例显示了 copyTree 方法的使用。

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)); 

上述程序生成以下输出

命令 − 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
广告