设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 服务器 > 系统 > 正文

遍历某路径下的所有文件

发布时间:2021-01-05 16:01 所属栏目:52 来源:网络整理
导读: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;
}

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读