前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VC遍历访问目录下的文件

VC遍历访问目录下的文件

作者头像
charlee44
发布2019-08-13 10:46:48
1.1K0
发布2019-08-13 10:46:48
举报

访问目录文件夹下的文件是经常需要的操作,C/C++和win32接口都没有提供直接调用的函数。在这里总结了几个经常用到的函数,通过MFC的CFileFind函数递归遍历实现,包括以下几个功能函数:

  1. 查找目录下所有的文件夹;
  2. 查找目录下所有的文件(不遍历目录的目录);
  3. 查找目录下所有的文件(遍历目录的目录) ;
  4. 查找目录下某一类型文件 (不遍历目录的目录);
  5. 查找目录下某一类型文件 (遍历目录的目录);
//查找目录下所有的文件夹
void FindFolder(string dir, vector<string> &folderPath)
{
    CFileFind fileFinder;
    CString filePath = CString(dir.c_str()) + _T("\\*.*");

    BOOL bFinished = fileFinder.FindFile(filePath);
    while (bFinished)  //每次循环对应一个类别目录
    {
        bFinished = fileFinder.FindNextFile();
        if (fileFinder.IsDirectory() && !fileFinder.IsDots())  //fileFinder.IsDots()识别"."文件和".."文件
        {
            CString filePath = fileFinder.GetFilePath();
            folderPath.push_back(filePath.GetBuffer());
            filePath.ReleaseBuffer();
        }
    }

    fileFinder.Close();
}

//查找目录下所有的文件(不遍历目录的目录)  
void FindDirFileNoFormat(string dir, vector<string> &filePath)
{
    CFileFind fileFinder;
    CString path = CString(dir.c_str()) + _T("\\*.*");

    BOOL bFinished = fileFinder.FindFile(path);
    while (bFinished)  //每次循环对应一个类别目录
    {
        bFinished = fileFinder.FindNextFile();
        if (fileFinder.IsDirectory() || fileFinder.IsDots())  //fileFinder.IsDots()识别"."文件和".."文件
        {
            continue;
        }
        else
        {
            CString findPath = fileFinder.GetFilePath();
            filePath.push_back(findPath.GetBuffer());
            findPath.ReleaseBuffer();
        }
    }

    fileFinder.Close();
}

//查找目录下所有的文件(遍历目录的目录)   
void FindAllFileNoFormat(string dir, vector<string> &filePath)
{
    CFileFind fileFinder;
    CString path = CString(dir.c_str()) + _T("\\*.*");

    BOOL bFinished = fileFinder.FindFile(path);
    while (bFinished)  //每次循环对应一个类别目录
    {
        bFinished = fileFinder.FindNextFile();

        // 跳过 . 和 .. ; 否则会陷入无限循环中!!!
        if (fileFinder.IsDots())
        {
            continue;
        }

        //
        if (fileFinder.IsDirectory())
        {
            CString findPath = fileFinder.GetFilePath();
            string subdir = findPath.GetBuffer();
            FindAllFileNoFormat(subdir, filePath);
            findPath.ReleaseBuffer();
        }
        else
        {
            CString findPath = fileFinder.GetFilePath();
            filePath.push_back(findPath.GetBuffer());
            findPath.ReleaseBuffer();
        }
    }

    fileFinder.Close();
}

// 查找目录下某一类型文件 (不遍历目录的目录)
void FindDirFile(string dir, string format, vector<string> &filePath)
{
    CFileFind fileFinder;
    CString path = CString(dir.c_str()) + _T("\\*") + CString(format.c_str());

    BOOL bFinished = fileFinder.FindFile(path);
    while (bFinished)  //每次循环对应一个类别目录
    {
        bFinished = fileFinder.FindNextFile();
        if (fileFinder.IsDirectory() && !fileFinder.IsDots())  //fileFinder.IsDots()识别"."文件和".."文件
        {
            continue;
        }
        else
        {
            CString findPath = fileFinder.GetFilePath();
            filePath.push_back(findPath.GetBuffer());
            findPath.ReleaseBuffer();
        }
    }

    fileFinder.Close();
}

//得到文件路径的格式后缀
string GetPathFormat(string filePath)
{
    string format = filePath;
    size_t p = filePath.find_last_of('.');
    if (p == -1)
    {
        return string();
    }
    format.erase(0, p);
    return format;
}

// 查找目录下某一类型文件 (遍历目录的目录)    
void FindDirAllFileEx(string dir, vector<string> &format, vector<string>& filePath)
{
    CFileFind fileFinder;
    CString path = CString(dir.c_str()) + _T("\\*.*");

    BOOL bFinished = fileFinder.FindFile(path);
    while (bFinished)  //每次循环对应一个类别目录
    {
        bFinished = fileFinder.FindNextFile();

        // 跳过 . 和 .. ; 否则会陷入无限循环中!!!
        if (fileFinder.IsDots())
        {
            continue;
        }

        if (fileFinder.IsDirectory())
        {
            CString findPath = fileFinder.GetFilePath();
            string subdir = findPath.GetBuffer();
            FindDirAllFileEx(subdir, format, filePath);
            findPath.ReleaseBuffer();
        }
        else
        {
            //获取文件类型
            CString findPath = fileFinder.GetFilePath();
            string file = findPath.GetBuffer();
            string postfix = GetPathFormat(file);

            bool flag = false;
            for (auto it : format)
            {
                if (_stricmp(it.c_str(), postfix.c_str()) == 0)
                {
                    flag = true;
                    break;
                }
            }

            if (flag)
            {
                filePath.push_back(file);
            }

            findPath.ReleaseBuffer();
        }
    }

    fileFinder.Close();
}

有一点值得注意的是我这里函数的参数都封装成STL的string,在多字节下可以直接使用,在unicode下需要稍微修改下CString与string的转换。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档