1. 思路
使用FindFirstFile、FindNextFile函数,来遍历目录。结果保存在WIN32_FIND_DATA结构体中。将查询结果的dwFileAttributes和FILE_ATTRIBUTE_DIRECTORY取与操作,判断是否为目录。
2. 示例代码
#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
int main()
{
HANDLE hRes;
WIN32_FIND_DATA data;
LPCSTR IpFileName;
char tmpDir[MAX_PATH] = "";
strcat(tmpDir,"E:\\ubuntuShare");
strcat(tmpDir,"\\*");
IpFileName = tmpDir;
hRes = FindFirstFileEx(IpFileName,FindExInfoStandard,&data,FindExSearchNameMatch,NULL,0);
while(FindNextFile(hRes,&data))
{
if(strcmp(data.cFileName,".") ==0 || strcmp(data.cFileName,"..") == 0)
continue;
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
printf("%s 是目录.\n",data.cFileName);
}
else
{
printf("%s 是文件.\n",data.cFileName);
}
}
return 0;
}