69,375
社区成员
发帖
与我相关
我的任务
分享
#define FILE_PATH "E:/demo.txt"
char *StrToUpper(char *str)
{
char *p = str;
while (*p)
{
if (*p >= 'a' && *p <= 'z')
{
*p -= 'a' - 'A';
}
p++;
}
return str;
}
char *GetExtension(char *file)
{
char *p = file;
while (*p)
{
p++;
}
while (*p != '.')
{
p--;
}
return p;
}
void GetFileBy_findnext(char *path, char *filter, char *extension)
{
FILE *fp; //文件指针
fp = fopen(FILE_PATH, "wt+");
// 判断文件是否能够正确创建/打开
if (fp == NULL) {
perror(FILE_PATH);
exit(1);
}
struct _finddata_t find_data;
long handle;
char buf[1000];
char search[1000];
char *p;
strcpy(buf, path);
p = buf;
while (*p)
{
p++;
}
p--;
if (*p != '\\')
{
strcat(buf, "\\");
}
strcpy(search, buf);
strcat(search, filter);
handle = _findfirst(search, &find_data);
if (handle == -1)
{
_findclose(handle);
return;
}
while (!_findnext(handle, &find_data))
{
if (strcmp(find_data.name, ".."))
{
if (find_data.attrib & _A_SUBDIR)
{
char buf1[1000];
char buf2[] = "Windows";
strcpy(buf1, find_data.name);
if (!strcmp(StrToUpper(buf1), StrToUpper(buf2)))
{
continue;
}
strcpy(buf1, buf);
strcat(buf1, find_data.name);
GetFileBy_findnext(buf1, filter, extension);
}
else
{
char buf1[100];
char buf2[100];
strcpy(buf1, GetExtension(find_data.name));
strcpy(buf2, GetExtension(extension));
if (!strcmp(StrToUpper(buf1), StrToUpper(buf2)))
{
fprintf(fp, "%s%s\n", buf, find_data.name);
printf("%s%s\n", buf, find_data.name);
//fflush(fp);
}
}
}
}
fclose(fp);
_findclose(handle);
}
int main()
{
GetFileBy_findnext("C:\\", "*.*", "*.exe");
}