您的位置:首页 > 编程语言 > PHP开发

[批处理php]对指定目录下的文件目录批处理,可选择处理目录的深度

2005-10-25 09:36 796 查看
class listfile{

var $fileFunc=null; //文件操作函数,用于execute里的调用
var $dirFunc=null; //目录操作函数
var $tag = 0; //tag 0,处理文件,1,处理目录,2,既处理文件也处理目录。
var $deep = 1;//目录深度,为1,表示第一层。
var $extname=null; //有效文本文件的扩展名。

function listfile()
{
echo 'initialize filetree
';
} // end func

/** 函数 execute( $dirName = null )
* 功能 对目录下所有文件及子目录下所有文件进行操作
* 参数 $dirName 目录名称
*/
function execute( $dirName = null ,$dirdeep = 1)
{
if( empty( $dirName ) )
exit( "IBFileSystem: directory is empty." );

if( is_dir( $dirName ) )
{
if( $dh = opendir( $dirName ) )
{
while( ( $file = readdir( $dh ) ) !== false )
{
if( $file != "." && $file != ".." )
{
$filePath = $dirName . "/" . $file;
if( is_dir( $filePath ) )//为目录,递归
{
if ($this->tag==1 || $this->tag==2) {
eval($this->dirFunc);
}
if ($this->deep>1) { //控制处理的目录深度
if ($dirdeep<$this->deep) {
$dirdeep++;
$this->execute( $filePath ,$dirdeep);
}
}
}
else//为文件,进行处理
{
if ($this->tag==0 || $this->tag==2) {
//如果包括全部文件
if ($this->extname=="*") {
eval($this->fileFunc); //echo"{$filePath}";echo"\n";
}
else {
$dotpos = strrpos($file,".");
if ($dotpos===false) {

}
else {
$extname = substr($file,$dotpos+1);
$pos = strpos($this->extname,$extname);
if ($pos===false) {

}
else {
eval($this->fileFunc); //echo"{$filePath}";echo"\n";
}
}
}
}
} //文件处理结束
}
}
closedir( $dh );

}
else
{
exit( "IBFileSystem: can not open directory $dirName.");
}

//返回当前的$tree
//return $tree;
}

else
{
exit( "IBFileSystem: $dirName is not a directory.");
}
}//

/**
* mkdirp is used to instead of mkdir ,mkdirp can create deep multiple directory.
*/
function mkdirp($target) {
// If the path already exists && is a directory, all is well.
// If the path is not a directory, we've a problem.
if (file_exists($target)) {
if (!is_dir($target)) return false;
else return true;
}

// Attempting to create the directory may clutter up our display.
if ( @mkdir($target) ) return true;

// If the above failed, attempt to create the parent node, then try again.
if ( $this->mkdirp(dirname($target)) ) return $this->mkdirp($target);

return false;
}//

}//

/*
//examples
$fl = new listfile();
//$fl->mkdirp("d:/1/2/3/4/5");
$fl->extname ="*";
$fl->tag = 2; //列出所有子目录
$fl->deep = 3;//列出目录层次一级。即当前目录下的子目录。2级表示也列出2级子目录
$fl->fileFunc = "echo(\$filePath.\"
\\n\");";
$fl->dirFunc = "echo(\$filePath.\"
\\n\");";
$fl->execute("D:/tmp/");
*/
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: