先利用FindFirstFile创建文件句柄
再利用FindNextFile找到文件,利用其dwFileAttributes判断是否是文件夹,如果不是,则连着文件路径一起输出,如果是,则将文件路径与文件名拼接并递归遍历
#include <windows.h>
#include <stdio.h>
int Judge_Directory(DWORD Attribute) {
if ((Attribute & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
return 1;
}
else {
return 0;
}
}
char* Joint(char* PathA, char* FileName) {
int i, j;
char Path[1000];
for (i = 0; PathA[i] != '*' && PathA[i] != '\0' && PathA[i] != '.'; i++) {
Path[i] = PathA[i];
}
for (j = 0; FileName[j] != '\0'; j++, i++)Path[i] = FileName[j];
Path[i] = '\\';
Path[i + 1] = '*';
Path[i + 2] = '\0';
return Path;
}
void Ergodic_Dictory(char* FileName){
WIN32_FIND_DATA p;
HANDLE h = FindFirstFileA((char*)FileName, &p);
while (FindNextFileA(h, &p)) {
char* name = (char*)p.cFileName;
char pathA[1000] = { 0 };
for (int i = 0; FileName[i] != '*'; i++)pathA[i] = FileName[i];
if (name[0] != '.') {
printf("%s%s\n", pathA, name);
}
if (Judge_Directory(p.dwFileAttributes) && name[0]!='.') {
char file[1000] = { 0 };
char* fileA = Joint(FileName, (char*)p.cFileName);
for (int i = 0; fileA[i] != '\0'; i++)file[i] = fileA[i];
Ergodic_Dictory(file);
}
}
}
void Ergodic_DictoryA(char* path) {
char pathA[1000] = { 0 };
for (int i = 0; path[i] != '\0'; i++)pathA[i] = path[i];
Ergodic_Dictory(pathA);
}
int main() {
Ergodic_DictoryA("g:\\tools\\*");
}