C实现目录的搜索与文件查找

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

标题:C实现目录的搜索与文件查找 [转贴]
发布: cboy 点击: 401 更新时间:2006-5-30


/*在当前及其所有子目录下搜索指定的文件,命令行模式*/
/*使用方法:编译成功后,可执行文件名为search.exe,在DOS下输入search /?即可获得帮助*/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PRF printf

int level=0,flag=1,dirs=0,over=0;/*当前目录的深度为0*/
struct ffblk d[10]; /*定义10级目录的深度*/
char buffer[MAXPATH];
FILE *fp;

void cd(char *direc)/*进入选定目录*/
{
struct ffblk f;
int done;
done=findfirst(direc,&f,31);
do
{
if((f.ff_attrib&(1<<4))!=16) /*找不到目录*/
{
PRF(" Cannot find diretory \"%s\"\n",direc);
return;
}
if(!done)
done=findnext(&f);
}while(!done);
strcpy(buffer,direc); /*将输入的目录名赋给buffer*/
chdir(buffer);
getcwd(buffer,MAXPATH);
}

void cdup()/*返回到上层目录*/
{
char str_1[MAXPATH], *p;
int n,m=0,i;
strcpy(str_1,buffer);
n=strlen(str_1); /*n存储工作目录buffer字符串的长度*/
p=str_1+n-1; /*使指针指向字符串的最后一个字符*/
for(i=0;iif(str_1=='\\')
++m;
if(m==1)/*如果是第二级目录则将盘符后第三个赋为结束符*/
str_1[3]='\0';
else /*如果是2级以上目录则进行另外一种算法*/
{
while((*p) != '\\')
p--;
*p='\0'; /*在指针指向str_1的地方重新赋值*/
}
strcpy(buffer,str_1);
chdir(buffer);/*更改工作目录*/
getcwd(buffer,MAXPATH); /*更改目录后,重新给buffer赋值,使其为当前目录*/
}

void Creat_List() /*创建目录列表*/
{
int done,i;
if(over) /*如果目录列表文件创建完毕则返回,是递归返回*/
return;
if(flag) /*flag用于判断是否在一目录中重新搜索,1为是,0为不是*/
done=findfirst("*",&d[level],31);
else
done=findnext(&d[level]);
while(!done)
{
if((d[level].ff_attrib&(1<<4))==16)/*如果是目录,注1*/
if(strcmp(d[level].ff_name,".")!=0&&strcmp(d[level].ff_name,"..")!=0)
{
for(i=0;ifprintf(fp,"%s\\",d.ff_name);
fprintf(fp,"%s\n",d[level].ff_name);
cd(d[level].ff_name);
flag=1;
++level;
Creat_List();
}
done=findnext(&d[level]);
}
cdup();
flag=0;
if(level>0)-

-level;
if(level==0&&dirs>0)--dirs;
if(level==0&&dirs==0) /*如果在深度为0的目录中搜索完毕,则退出*/
over=1;
else
Creat_List();
}

void search(int argc,char *argv[]) /*查找指定文件*/
{
char path[MAXPATH],buf_tmp[MAXPATH];
int done,find=0,count=0,attr=33,write=0;
FILE *fp1=NULL;
struct ffblk f;
if(argc>2) /*有两个以上的参数时*/
if(!strcmp(argv[2],"-h")||!strcmp(argv[2],"-H"))
attr=2;
else if(!strcmp(argv[2],"-a")||!strcmp(argv[2],"-A"))
attr=31;
else if(!strcmp(argv[2],"-o")||!strcmp(argv[2],"-O"))
attr=33;
else if(argc==3) /*没有指定参数*/
{
if((fp1=fopen(argv[2],"w+"))==NULL)
{
PRF(" Creat export file error!\n");
return;
}
write=1; /*是否导出结果到文件中*/
}
else if(argc==4) /*参数指定错误*/
{
PRF(" Para Error!\n");
return;
}
if(argc==4)
{
if((fp1=fopen(argv[3],"w+"))==NULL)
{
PRF(" Creat export file error!\n");
return;
}
write=1;
}

getcwd(buffer,MAXPATH);/*取得当前目录*/
strcpy(buf_tmp,buffer);
done=findfirst("*",&f,31);/*搜索当前目录中的所有子目录并记数*/
while(!done)
{
if((f.ff_attrib&(1<<4))==16)
if(strcmp(f.ff_name,".")!=0&&strcmp(f.ff_name,"..")! =0)
++dirs;
done=findnext(&f);
}
fp=fopen("dirs.tmp","w+");
fprintf(fp,"%s\n",buffer); /*输入当前目录*/
printf("Creating directories list now,please wait------\n");
Creat_List();

PRF("\n\nNow start to search files,please wait------\n");
rewind(fp);
while(!feof(fp))
{
fscanf(fp,"%s",path);
cd(path);
done=findfirst(argv[1],&f,31);
while(!done)
{
find=1; /*如果有符合条件的文件则置find为1*/
if(fp1==NULL&&wherey()==25)
{
PRF("------more------");
getch();
clrscr();
}
if(attr==31)
{
(!write)?PRF("%-15s%s\n",f.ff_name,buffer):
fprintf(fp1,"%-15s%s\n",f.ff_name,buffer);
++c

ount;
}
else if(attr==2&&(f.ff_attrib&(1<<1)))
{
(!write)?PRF("%-15s%s\n",f.ff_name,buffer):
fprintf(fp1,"%-15s%s\n",f.ff_name,buffer);
++count;
}
else if(attr==33&&((f.ff_attrib&(1<<0))||(f.ff_ attrib&(1<<5))))
{
(!write)?PRF("%-15s%s\n",f.ff_name,buffer):
fprintf(fp1,"%-15s%s\n",f.ff_name,buffer);
++count;
}
done=findnext(&f);
}
strcpy(buffer,buf_tmp);/*返回到最开始的目录*/
chdir(buffer);
getcwd(buffer,MAXPATH);
}
fclose(fp);
if(!find)
PRF("There are no files accroding to you conditions!\n");
else if(!write)
PRF("\nFind over!\n");
if(write)
{
fprintf(fp1,"\n There %s %d files!\n",count>1?"are":"is",count);
fprintf(fp1,"\n\n********Written By MaBiqiang********");
fclose(fp1);
PRF("Export to \"%s\" successfully!\n",argv[argc-1]);
}
}

void main(int argc,char *argv[])
{
int done;
if(argc<2||argc>4)
{
PRF("Para Error\n");
exit(0);
}
if(!strcmp(argv[1],"/?"))
{
PRF(" This programme is used to searching files with setted paras ");
PRF("in all directorys of the current direcoty!You can used following:\n");
PRF("\n search filename [-h][-a][-o] [ex_file]\n\n");
PRF(" -h only search hidden files\n");
PRF(" -a search all files\n");
PRF(" -o the default para,search ordinary files\n");
PRF(" ex_file export results to a file\n");
return;
}
PRF("This programme is uesd to search files!\n\n");
PRF("***************Written By MaBiqiang***************\n");
PRF("*************** 2004 - 10 - 15 ***************\n\n");
search(argc,argv);
remove("dirs.tmp"); /*删除临时目录文件*/
}

相关文档
最新文档