[批处理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/");
*/
?>
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/");
*/
?>
相关文章推荐
- [Shell和批处理]_[枚举指定目录下的文件并处理]
- [Shell和批处理]_[枚举指定目录下的文件并处理]
- php远程抓取(下载)文件到本项目指定目录中
- nginx配置文件中去掉指定目录下面php的后缀
- dos批处理:列出指定目录下所有文件
- php实现删除指定目录下相关文件的方法
- PHP在获取指定目录下的目录,在获取的目录下面再创建文件,多平台!
- 如何指定文件夹处理目录下文件
- php 实现 从一个目录中 把指定类型文件 重新组装到新的目录
- PHP删除目录及目录下所有文件或删除指定文件
- php带密码功能并下载远程文件保存本地指定目录 修改加强版
- 禁止指定目录执行php文件
- c#winform选择文件,文件夹,打开指定目录方法
- c#winform选择文件,文件夹,打开指定目录方法
- php上传文件后无法移动到指定目录的解决
- c#winform选择文件,文件夹,打开指定目录方法
- php实现远程网络文件下载到服务器指定目录(方法二)
- PHP处理Linux中生成文件目录及读取内容遍历应用
- PHP遍历指定目录,并存储目录内所有文件属性信息