您的位置:首页 > 编程语言 > C语言/C++

c++搜索目录下的所有文件

2010-06-18 23:26 204 查看
#include "stdlib.h"

#include "direct.h"

#include "string.h"

#include "io.h"

#include "stdio.h"

#include "iostream.h"

class
CBrowseDir

{

protected
:

//存放初始目录的绝对路径,以'/'结尾

char
m_szInitDir[_MAX_PATH];

public
:

//缺省构造器

CBrowseDir();

//设置初始目录为dir,如果返回false,表示目录不可用

bool
SetInitDir(
const

char
*dir);

//开始遍历初始目录及其子目录下由filespec指定类型的文件

//filespec可以使用通配符 * ?,不能包含路径。

//如果返回false,表示遍历过程被用户中止

bool
BeginBrowse(
const

char
*filespec);

protected
:

//遍历目录dir下由filespec指定的文件

//对于子目录,采用迭代的方法

//如果返回false,表示中止遍历文件

bool
BrowseDir(
const

char
*dir,
const

char
*filespec);

//函数BrowseDir每找到一个文件,就调用ProcessFile

//并把文件名作为参数传递过去

//如果返回false,表示中止遍历文件

//用户可以覆写该函数,加入自己的处理代码

virtual

bool
ProcessFile(
const

char
*filename);

//函数BrowseDir每进入一个目录,就调用ProcessDir

//并把正在处理的目录名及上一级目录名作为参数传递过去

//如果正在处理的是初始目录,则parentdir=NULL

//用户可以覆写该函数,加入自己的处理代码

//比如用户可以在这里统计子目录的个数

virtual

void
ProcessDir(
const

char
*currentdir,
const

char
*parentdir);

};

CBrowseDir::CBrowseDir()

{

//用当前目录初始化m_szInitDir

getcwd(m_szInitDir,_MAX_PATH);

//如果目录的最后一个字母不是'/',则在最后加上一个'/'

int
len=strlen(m_szInitDir);

if
(m_szInitDir[len-1] !=
'//'
)

strcat(m_szInitDir,"//"
);

}

bool
CBrowseDir::SetInitDir(
const

char
*dir)

{

//先把dir转换为绝对路径

if
(_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)

return

false
;

//判断目录是否存在

if
(_chdir(m_szInitDir) != 0)

return

false
;

//如果目录的最后一个字母不是'/',则在最后加上一个'/'

int
len=strlen(m_szInitDir);

if
(m_szInitDir[len-1] !=
'//'
)

strcat(m_szInitDir,"//"
);

return

true
;

}

bool
CBrowseDir::BeginBrowse(
const

char
*filespec)

{

ProcessDir(m_szInitDir,NULL);

return
BrowseDir(m_szInitDir,filespec);

}

bool
CBrowseDir::BrowseDir(
const

char
*dir,
const

char
*filespec)

{

_chdir(dir);

//首先查找dir中符合要求的文件

long
hFile;

_finddata_t
fileinfo;

if
((hFile=_findfirst(filespec,&fileinfo)) != -1)

{

do

{

//检查是不是目录

//如果不是,则进行处理

if
(!(fileinfo.attrib & _A_SUBDIR))

{

char
filename[_MAX_PATH];

strcpy(filename,dir);

strcat(filename,fileinfo.name);

cout << filename << endl;

if
(!ProcessFile(filename))

return

false
;

}

} while
(_findnext(hFile,&fileinfo) == 0);

_findclose(hFile);

}

//查找dir中的子目录

//因为在处理dir中的文件时,派生类的ProcessFile有可能改变了

//当前目录,因此还要重新设置当前目录为dir。

//执行过_findfirst后,可能系统记录下了相关信息,因此改变目录

//对_findnext没有影响。

_chdir(dir);

if
((hFile=_findfirst(
"*.*"
,&fileinfo)) != -1)

{

do

{

//检查是不是目录

//如果是,再检查是不是 . 或 ..

//如果不是,进行迭代

if
((fileinfo.attrib & _A_SUBDIR))

{

if
(strcmp(fileinfo.name,
"."
) != 0 && strcmp

(fileinfo.name,".."
) != 0)

{

char
subdir[_MAX_PATH];

strcpy(subdir,dir);

strcat(subdir,fileinfo.name);

strcat(subdir,"//"
);

ProcessDir(subdir,dir);

if
(!BrowseDir(subdir,filespec))

return

false
;

}

}

} while
(_findnext(hFile,&fileinfo) == 0);

_findclose(hFile);

}

return

true
;

}

bool
CBrowseDir::ProcessFile(
const

char
*filename)

{

return

true
;

}

void
CBrowseDir::ProcessDir(
const

char

*currentdir,const

char
*parentdir)

{

}

//从CBrowseDir派生出的子类,用来统计目录中的文件及子目录个数

class
CStatDir:
public
CBrowseDir

{

protected
:

int
m_nFileCount;
//保存文件个数

int
m_nSubdirCount;
//保存子目录个数

public
:

//缺省构造器

CStatDir()

{

//初始化数据成员m_nFileCount和m_nSubdirCount

m_nFileCount=m_nSubdirCount=0;

}

//返回文件个数

int
GetFileCount()

{

return
m_nFileCount;

}

//返回子目录个数

int
GetSubdirCount()

{

//因为进入初始目录时,也会调用函数ProcessDir,

//所以减1后才是真正的子目录个数。

return
m_nSubdirCount-1;

}

protected
:

//覆写虚函数ProcessFile,每调用一次,文件个数加1

virtual

bool
ProcessFile(
const

char
*filename)

{

m_nFileCount++;

return
CBrowseDir::ProcessFile(filename);

}

//覆写虚函数ProcessDir,每调用一次,子目录个数加1

virtual

void
ProcessDir

(const

char
*currentdir,
const

char
*parentdir)

{

m_nSubdirCount++;

CBrowseDir::ProcessDir(currentdir,parentdir);

}

};

void
main()

{

//获取目录名

char
buf[256];

printf("请输入要统计的目录名:"
);

gets(buf);

//构造类对象

CStatDir statdir;

//设置要遍历的目录

if
(!statdir.SetInitDir(buf))

{

puts("目录不存在。"
);

return
;

}

//开始遍历

statdir.BeginBrowse("*.*"
);

printf("文件总数: %d/n子目录总数:%d/n"
,statdir.GetFileCount(),statdir.GetSubdirCount());

}

#include "stdlib.h"
#include "direct.h"
#include "string.h"
#include "io.h"
#include "stdio.h"
#include "iostream.h"

class CBrowseDir
{
protected:
//存放初始目录的绝对路径,以'/'结尾
char m_szInitDir[_MAX_PATH];

public:
//缺省构造器
CBrowseDir();

//设置初始目录为dir,如果返回false,表示目录不可用
bool SetInitDir(const char *dir);

//开始遍历初始目录及其子目录下由filespec指定类型的文件
//filespec可以使用通配符 * ?,不能包含路径。
//如果返回false,表示遍历过程被用户中止
bool BeginBrowse(const char *filespec);

protected:
//遍历目录dir下由filespec指定的文件
//对于子目录,采用迭代的方法
//如果返回false,表示中止遍历文件
bool BrowseDir(const char *dir,const char *filespec);

//函数BrowseDir每找到一个文件,就调用ProcessFile
//并把文件名作为参数传递过去
//如果返回false,表示中止遍历文件
//用户可以覆写该函数,加入自己的处理代码
virtual bool ProcessFile(const char *filename);

//函数BrowseDir每进入一个目录,就调用ProcessDir
//并把正在处理的目录名及上一级目录名作为参数传递过去
//如果正在处理的是初始目录,则parentdir=NULL
//用户可以覆写该函数,加入自己的处理代码
//比如用户可以在这里统计子目录的个数
virtual void ProcessDir(const char *currentdir,const char *parentdir);
};

CBrowseDir::CBrowseDir()
{
//用当前目录初始化m_szInitDir
getcwd(m_szInitDir,_MAX_PATH);

//如果目录的最后一个字母不是'/',则在最后加上一个'/'
int len=strlen(m_szInitDir);
if (m_szInitDir[len-1] != '//')
strcat(m_szInitDir,"//");
}

bool CBrowseDir::SetInitDir(const char *dir)
{
//先把dir转换为绝对路径
if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)
return false;

//判断目录是否存在
if (_chdir(m_szInitDir) != 0)
return false;

//如果目录的最后一个字母不是'/',则在最后加上一个'/'
int len=strlen(m_szInitDir);
if (m_szInitDir[len-1] != '//')
strcat(m_szInitDir,"//");

return true;
}

bool CBrowseDir::BeginBrowse(const char *filespec)
{
ProcessDir(m_szInitDir,NULL);
return BrowseDir(m_szInitDir,filespec);
}

bool CBrowseDir::BrowseDir(const char *dir,const char *filespec)
{
_chdir(dir);

//首先查找dir中符合要求的文件
long hFile;
_finddata_t fileinfo;
if ((hFile=_findfirst(filespec,&fileinfo)) != -1)
{
do
{
//检查是不是目录
//如果不是,则进行处理
if (!(fileinfo.attrib & _A_SUBDIR))
{
char filename[_MAX_PATH];
strcpy(filename,dir);
strcat(filename,fileinfo.name);
cout << filename << endl;
if (!ProcessFile(filename))
return false;
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
//查找dir中的子目录
//因为在处理dir中的文件时,派生类的ProcessFile有可能改变了
//当前目录,因此还要重新设置当前目录为dir。
//执行过_findfirst后,可能系统记录下了相关信息,因此改变目录
//对_findnext没有影响。
_chdir(dir);
if ((hFile=_findfirst("*.*",&fileinfo)) != -1)
{
do
{
//检查是不是目录
//如果是,再检查是不是 . 或 ..
//如果不是,进行迭代
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name,".") != 0 && strcmp
(fileinfo.name,"..") != 0)
{
char subdir[_MAX_PATH];
strcpy(subdir,dir);
strcat(subdir,fileinfo.name);
strcat(subdir,"//");
ProcessDir(subdir,dir);
if (!BrowseDir(subdir,filespec))
return false;
}
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
return true;
}

bool CBrowseDir::ProcessFile(const char *filename)
{
return true;
}

void CBrowseDir::ProcessDir(const char
*currentdir,const char *parentdir)
{
}

//从CBrowseDir派生出的子类,用来统计目录中的文件及子目录个数
class CStatDir:public CBrowseDir
{
protected:
int m_nFileCount;   //保存文件个数
int m_nSubdirCount; //保存子目录个数

public:
//缺省构造器
CStatDir()
{
//初始化数据成员m_nFileCount和m_nSubdirCount
m_nFileCount=m_nSubdirCount=0;
}

//返回文件个数
int GetFileCount()
{
return m_nFileCount;
}

//返回子目录个数
int GetSubdirCount()
{
//因为进入初始目录时,也会调用函数ProcessDir,
//所以减1后才是真正的子目录个数。
return m_nSubdirCount-1;
}

protected:
//覆写虚函数ProcessFile,每调用一次,文件个数加1
virtual bool ProcessFile(const char *filename)
{
m_nFileCount++;
return CBrowseDir::ProcessFile(filename);
}

//覆写虚函数ProcessDir,每调用一次,子目录个数加1
virtual void ProcessDir
(const char *currentdir,const char *parentdir)
{
m_nSubdirCount++;
CBrowseDir::ProcessDir(currentdir,parentdir);
}
};

void main()
{
//获取目录名
char buf[256];
printf("请输入要统计的目录名:");
gets(buf);

//构造类对象
CStatDir statdir;

//设置要遍历的目录
if (!statdir.SetInitDir(buf))
{
puts("目录不存在。");
return;
}

//开始遍历
statdir.BeginBrowse("*.*");
printf("文件总数: %d/n子目录总数:%d/n",statdir.GetFileCount(),statdir.GetSubdirCount());
}


相关函数的解释

_fullpath(char*abspath,char* relpath,size_t maxsize);

参数解释


absPath

Pointer to a buffer containing the absolute or full path name, or NULL.

relPath

Relative path name.

maxLength

Maximum length of the absolute path name buffer (absPath
). This length is in bytes for _fullpath
but in wide characters (wchar_t
) for _wfullpath
.

返回值

Each of these functions returns a pointer to a buffer containing the absolute path name (absPath
). If there is an error (for example, if the value passed in relPath
includes a drive letter that is not valid or cannot be found, or if the length of the created absolute path name (absPath
) is greater than maxLength
), the function returns NULL
.



Remarks

The _fullpath
function expands the relative path name in relPath
to its fully qualified or absolute path and stores this name in absPath
.
If absPath
is NULL, malloc
is used to allocate a buffer of sufficient length to hold the path
name. It is the responsibility of the caller to free this buffer. A
relative path name specifies a path to another location from the
current location (such as the current working directory: "."). An
absolute path name is the expansion of a relative path name that states
the entire path required to reach the desired location from the root of
the file system. Unlike _makepath
, _fullpath
can be used to obtain the absolute path name for relative paths (relPath
) that include "./" or "../" in their names.

For
example, to use C run-time routines, the application must include the
header files that contain the declarations for the routines. Each
header file include statement references the location of the file in a
relative manner (from the application's working directory):

改变当前的工作目录到指定的目录中

_chdir, _wchdir

Changes the current working directory.

int _chdir(

const char *dirname

);

int _wchdir(

const wchar_t *dirname

);


Parameters

dirname

Path of new working directory.



Return Value

These
functions return a value of 0 if successful. A return value of –1
indicates failure. If the specified path could not be found, errno
is set to ENOENT
. If dirname
is NULL, the invalid parameter handler is invoked, as described in Parameter Validation
. If execution is allowed to continue, errno
is set to EINVAL
and the function returns -1.



Remarks

The _chdir
function changes the current working directory to the directory specified by dirname
. The dirname
parameter must refer to an existing directory. This function can change
the current working directory on any drive. If a new drive letter is
specified in dirname
, the default drive
letter is changed as well. For example, if A is the default drive
letter and /BIN is the current working directory, the following call
changes the current working directory for drive C and establishes C as
the new default drive:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: