PhantomJS copyTree 复制文件夹与子目录使用示例


发布日期 : 2021-11-26 21:41:28 UTC

访问量: 10 次浏览

PhantomJS copyTree

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

如果有任何文件或文件夹在复制过程中失败,它将抛出一个错误 – “无法复制目录树源于目的地”,并且执行将被挂起。

语法

copyTree 的语法如下所示 –

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