windows编程使用递归方法遍历文件的方法
2012-09-25 10:19
447 查看
在论坛经常碰到初学者不知道如何遍历文件,其实很简单,只要使用下面的代码就可以了。
void ChkRecurse(LPCTSTR pstr /* = NULL */)
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = FALSE;
try
{
bWorking = finder.FindFile(strWildcard);
}
catch (CException* e)
{
char szCause[255] = {0};
e->GetErrorMessage(szCause, 255);
Log->LogOutEx(szCause);
}
while (bWorking)
{
try
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots()) // '.' and '..'
continue;
if (finder.IsSystem())// 系统文件
continue;
else if (finder.IsDirectory()) //目录
{
CString str = finder.GetFilePath();
CString str1;
GetDirName(str,str1);
if ( str1.GetLength() == 12 && _is_num(str1) )
{
ChkRecurse(str);//回调
}
else
{
Sleep(20);
continue;
}
}
else
{//检查文件
//Do your work...
}
}
catch (CException* e)
{
char szCause[255] = {0};
e->GetErrorMessage(szCause, 255);
Log->LogOutEx(szCause);
}
}
finder.Close();
}
有不明白的地方欢迎评论、留言。
void ChkRecurse(LPCTSTR pstr /* = NULL */)
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = FALSE;
try
{
bWorking = finder.FindFile(strWildcard);
}
catch (CException* e)
{
char szCause[255] = {0};
e->GetErrorMessage(szCause, 255);
Log->LogOutEx(szCause);
}
while (bWorking)
{
try
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots()) // '.' and '..'
continue;
if (finder.IsSystem())// 系统文件
continue;
else if (finder.IsDirectory()) //目录
{
CString str = finder.GetFilePath();
CString str1;
GetDirName(str,str1);
if ( str1.GetLength() == 12 && _is_num(str1) )
{
ChkRecurse(str);//回调
}
else
{
Sleep(20);
continue;
}
}
else
{//检查文件
//Do your work...
}
}
catch (CException* e)
{
char szCause[255] = {0};
e->GetErrorMessage(szCause, 255);
Log->LogOutEx(szCause);
}
}
finder.Close();
}
有不明白的地方欢迎评论、留言。
相关文章推荐
- 使用递归方法遍历文件夹下的所有文件
- 递归的一些方法使用:深度遍历路径中的所有文件和目录,删除一个有文件的文件夹
- Android编程使用HTTP协议与TCP协议实现上传文件的方法
- Android编程使用WebView实现文件下载功能的两种方法
- PHP递归遍历指定文件夹内的文件实现方法
- windows winhttp的编程例子,但是头文件和库文件,我的机子环境上没有,只有下载了头文件和库文件后才可以使用。
- 使用DCMTK遍历读取DICOM文件所有Tag的方法
- PHP使用glob方法遍历文件夹下所有文件的实例
- c# 使用递归遍历所有文件目录
- 使用glob方法遍历文件夹下所有文件
- 二叉树前序、中序和后序的遍历方法(递归、用栈和使用线索化)
- 分别使用一般递归的方法和模板元编程实现台阶问题。
- Windows下使用标准Shell接口遍历文件和文件夹
- 使用DCMTK遍历读取DICOM文件所有Tag的方法
- Windows编程那些事----内存管理方法之二【内存映射文件】
- 使用git从mac拉取代码到windows合并后,未修改文件变为已修改的解决方法
- Windows下使用标准Shell接口遍历文件和文件夹(1)
- PHP递归遍历指定目录的文件并统计文件数量的方法
- C# static方法-使用迭代器循环遍历文件中的额行
- shell 递归遍历目录下的所有文件并统一改名的方法