|字号 订阅
/*说明:程序功能:遍历C、D盘并把信息保存到文件dir.txt中*/
#include <windows.h>
#include <stdio.h>
#include <conio.h>
void SearchDir(char *path, FILE* fp)//注意:这个path必须以'\'结尾
{
HANDLE hFind;
WIN32_FIND_DATA wfData;
char FullPath[MAX_PATH];
char subdir[MAX_PATH];
fprintf(fp,"Directory of %s\n", path);
strcpy(FullPath,path);
strcat(FullPath,"*.*");
hFind = FindFirstFile(FullPath,&wfData);
if(hFind == INVALID_HANDLE_VALUE)
{
fprintf(fp,"No file is found.\n");
return;
}
do
{
switch(wfData.dwFileAttributes)
{
case FILE_ATTRIBUTE_DIRECTORY://目录
if(strcmp(wfData.cFileName,".") && strcmp(wfData.cFileName,".."))//排除当前目录和上级目录
{
fprintf(fp,"%s\t\t\t\t\tdirectory\n",wfData.cFileName);
sprintf(subdir,"%s%s\\",path,wfData.cFileName);
SearchDir(subdir,fp);//递归调用,这是遍历的基础
}
break;
default:
fprintf(fp,"\t%s\t\t\t\tfile\n",wfData.cFileName);
break;
}
}while(FindNextFile(hFind,&wfData));
}
void main()
{
FILE *fp;
fp = fopen("dir.txt","wt");
if(fp==NULL)
{
printf("Unable to create output file!\npress any key to quit.\n");
getch();
return;
}
printf("Searching Volume C, please wait.");
SearchDir("C:\\",fp);
printf("...finished.\n");
printf("Searching Volume D, please wait.");
SearchDir("D:\\",fp);
printf("...finished.\n");
fclose(fp);
printf("Comamnd finished successfully!\nPress any key to view output.\n");
getch();
WinExec("notepad dir.txt",SW_SHOW);
}