代码
int IteratorFileNameOfDir(char* pDirName)
{
WIN32_FIND_DATAA struFindData;
HANDLE hFileHandle;
char szFilePathName[1024] = { 0 };
strcpy(szFilePathName, pDirName);
strcat(szFilePathName, "\\*.*");
hFileHandle = ::FindFirstFileA(szFilePathName, &struFindData);
if (INVALID_HANDLE_VALUE == hFileHandle)
{
std::cout << "iterator file name failed" << std::endl;
return -1;
}
while (::FindNextFileA(hFileHandle, &struFindData))
{
//排查父节点和指向当前的子节点
if (
(strcmp(struFindData.cFileName, ".") == 0) ||
(strcmp(struFindData.cFileName, "..") == 0))
{
continue;
}
std::cout << struFindData.cFileName << std::endl;
sprintf(szFilePathName, "%s/%s", pDirName, struFindData.cFileName);
std::string strFileName = szFilePathName;
//循环迭代
if (struFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
IteratorFileNameOfDir(szFilePathName);
}
}
return 0;
}
调用逻辑
char szDirName[] = "F:/opensource/osg/Production_3";
IteratorFileNameOfDir(szDirName);