C++ 遍历文件夹以及子文件夹下所有文件
CFileFind 所提供的方法进行文件夹以及子文件夹遍历时,经过测试会出现如果当前遍历的路径为盘符,且盘符中仅包含一个中文文件夹(文件夹名以汉字开头),此时遍历不到该文件夹。
所以采用以下方法(需要添加头#include "io.h" )
void GetAllFileFromPath(CString folderPath) { if (folderPath == _T("")) { return; } _finddata_t FileInfo; CString strfind = folderPath + "\*"; long Handle = _findfirst(strfind, &FileInfo); if (Handle == -1L) { _findclose(Handle); return; } do{ if (FileInfo.attrib & _A_SUBDIR) { if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0)) { CString newPath = folderPath + "\" + FileInfo.name; GetAllFileFromPath(newPath); } } else { CString strFindName = TMGetFileExt(FileInfo.name); { //此处记录文件 strFindName } } } while (_findnext(Handle, &FileInfo) == 0); _findclose(Handle); }
测试通过!
仅做笔记记录,请网友指正!
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 截取字符串使用省略号替代
- 下一篇: 关于文字内容溢出用点点点(…)省略号表示