加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

遍历某路径下的所有文件

发布时间:2020-12-14 05:58:46 所属栏目:Windows 来源:网络整理
导读:1.Windows API #include Windows.h int main(){ WIN32_FIND_DATA FindFileData = { 0 }; HANDLE h = FindFirstFile(TEXT( " F:BBC记录片*.* " ), FindFileData); if (INVALID_HANDLE_VALUE == h) return - 1 ; BOOL bRet = 1 ; int count = 0 ; while (b

1.Windows API

#include <Windows.h>
int main()
{
    WIN32_FIND_DATA FindFileData = { 0 };
    HANDLE h = FindFirstFile(TEXT("F:BBC记录片*.*"),&FindFileData);
    if (INVALID_HANDLE_VALUE == h)
        return -1;
    BOOL bRet = 1;
    int count = 0;
    while (bRet)
    {
        std::wstring sFilename = FindFileData.cFileName;
        bRet = FindNextFile(h,&FindFileData);
        ++count;
    }
    return 0;
}

2. MFC中的CFileFind类

void CTestMFCDlg::OnCfilefind() 
{
    // TODO: Add your control notification handler code here
    CListBox* pList = (CListBox*)GetDlgItem(IDC_LIST_FILE);
    CFileFind finder;
    BOOL bRet = finder.FindFile("C:*.*");
    while(bRet)
    {
        bRet = finder.FindNextFile();
        pList->AddString(finder.GetFileName());
    }
}

?

下面参考:https://blog.csdn.net/hisinwang/article/details/45725319

CFileFind fFinder;
BOOL bFind = fFinder.FindFile(TEXT("D:/*.*"));
while (bFind)
{
    bFind = fFinder.FindNextFile();

    //当前文件夹及上层文件夹(名称分别为..)-----------------
    if (fFinder.IsDots()) 
    {
        continue;
    }

    //子文件夹---------------------------------------------
    if(fFinder.IsDirectory()) 
    {
        CString cstrDirName = fFinder.GetFileName();  //directory name
        CString cstrDirPath = fFinder.GetFilePath();  //directory path
        continue;
    }

    //文件-------------------------------------------------
    CString cstrFileName = fFinder.GetFileName();   //file name
    CString cstrFilePath = fFinder.GetFilePath();   //file path
}

fFinder.Close();

?

3. C语言函数 _findfirst

#include <io.h>
#include <vector>
int main()
{
    _finddata64i32_t findData = { 0 };
    long handle =  _findfirst("F:BBC记录片*.*",&findData);
    if (-1 == handle)
        return 0;
    std::vector<std::string> vecFilename;
    vecFilename.push_back(findData.name);
    while (-1 != _findnext(handle,&findData))
    {
        vecFilename.push_back(findData.name);
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读