PhantomJS - removeTree



removeTree 方法用于删除给定文件夹中的所有文件和文件夹,最后删除文件夹本身。如果执行此过程时出现任何错误,它会抛出以下错误 -“无法删除目录树 PATH”并挂起执行。

语法

其语法如下 –

fs.removeTree(foldertodelete) 

示例

以下示例显示了 **removeTree** 方法如何工作。

var fs = require('fs'); 
var system = require('system'); 
var path = system.args[1]; 
console.log("Directory present : "+fs.isDirectory(path)); 

var a = fs.list(path); 
console.log("Listing the contents from the directory : " + JSON.stringify(a)); 
console.log("Removing directory "+path);  

var rd = fs.removeTree(path); 
console.log("Directory present "+fs.isDirectory(path)); 
phantom.exit();

上述程序产生了以下 **输出**。

Directory present : true 
Listing the contents from the directory : [".","..","examples","newfiles"] 
Removing directory removetree 
Directory present false
phantomjs_file_system_module_methods.htm
广告