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
廣告
© . All rights reserved.