|
一个文件搜索类,可以递归查找和模糊匹配的,花了点时间写了下,用一个链表保存文件信息。最后用releasefile释放这个链表。
- #include <iostream>
- #include "windows.h"
- using namespace std;
- class file{
- public:
- file(char *,int);
- ~file();
- void getfilename(char*);
- void getdir(char*);
- void getfullname(char*);
- void getext(char*);
- void getname(char*);
- int gettype();
- file *next;
- int getlength();
- private:
- char *fullname;
- int type;
- };
- class filesearch{
- public:
- file* search(char *,char *,int,int);
- file* subfolders(char *);
- file* subfiles(char *);
- file* subff(char *);
- void releasefile(file*);
- };
- file::file(char *path,int t){
- fullname=new char[strlen(path)+1];
- strcpy(fullname,path);
- type=t;
- }
- file::~file(){
- delete[] fullname;
- }
- void file::getfilename(char* name){
- int i=strlen(fullname)-1;
- while(i>=0){
- if(fullname[i]=='\\' || fullname[i]=='/'){
- strcpy(name,fullname+i+1);
- return;
- }
- i--;
- }
- strcpy(name,"");
- }
- void file::getdir(char* dir){
- char *s=new char[strlen(fullname)+1];
- getfilename(s);
- int c=strlen(fullname)-strlen(s)-1;
- strncpy(dir,fullname,c);
- dir[c]=0;
- delete[] s;
- }
- void file::getfullname(char* name){
- strcpy(name,fullname);
- }
- void file::getext(char* ext){
- char *s=new char[strlen(fullname)+1];
- getfilename(s);
- int i=strlen(s)-1;
- while(i>=0){
- if(s[i]=='.'){
- strcpy(ext,s+i+1);
- return;
- }
- i--;
- }
- strcpy(ext,"");
- delete[] s;
- }
- void file::getname(char* name){
- int i=strlen(fullname)-1;
- while(i>=0){
- if(fullname[i]=='\\' || fullname[i]=='/'){
- i++;
- int i1=0;
- while(fullname[i]){
-
- if(fullname[i]=='.'){
- name[i1]=0;
- return;
- }else{
- name[i1++]=fullname[i];
- }
- i++;
- }
- name[i1]=0;
- return;
-
- }
- i--;
- }
- strcpy(name,"");
- }
- int file::gettype(){
- return type;
- }
- int file::getlength(){
- return strlen(fullname);
- }
- file* filesearch::subff(char *path){
- WIN32_FIND_DATA data;
- char tpath[MAX_PATH];
- strcpy(tpath,path);
- strcat(tpath,"\\*");
- HANDLE h=FindFirstFile(tpath,&data);
- FindNextFile(h,&data);
- file *ff=0;
- file *head=0;
- while(FindNextFile(h,&data)){
- char name[MAX_PATH];
- strcpy(name,path);
- strcat(name,"\");
- strcat(name,data.cFileName);
- file* ff1=new file(name,data.dwFileAttributes);
- ff1->next=0;
- if(ff!=0){
- ff->next=ff1;
- ff=ff1;
- }else{
- ff=ff1;
- head=ff;
- }
-
- if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
- file* ff2=subff(name);
- if(ff2){
- ff1->next=ff2;
- while(ff2->next!=0){
- ff2=ff2->next;
- }
- ff=ff2;
- }
- }
-
- }
- FindClose(h);
- return head;
- }
- void filesearch::releasefile(file* f){
- while(f){
- file *f1=f;
- f=f->next;
- delete f1;
- }
- }
- file *filesearch::search(char *dir,char *name,int r,int reg){
- file *f=subff(dir);
- file *f1=0;
- file *head=0;
- if(f){
-
- if(r){
- if(reg){
- while(f){
- char a[MAX_PATH];
- f->getfilename(a);
- if(strstr(a,name)){
- char fullname[MAX_PATH];
- f->getfullname(fullname);
- file *ff2=new file(fullname,f->gettype());
- ff2->next=0;
- if(f1){
- f1->next=ff2;
- f1=ff2;
- }else{
- f1=ff2;
- head=f1;
- }
- }
- f=f->next;
- }
- }else{
- while(f){
- char a[MAX_PATH];
- f->getfilename(a);
- if(stricmp(a,name)==0){
- char fullname[MAX_PATH];
- f->getfullname(fullname);
- file *ff2=new file(fullname,f->gettype());
- ff2->next=0;
- if(f1){
- f1->next=ff2;
- f1=ff2;
- }else{
- f1=ff2;
- head=f1;
- }
- }
- f=f->next;
- }
- }
- }else{
- if(reg){
- while(f){
- char a[MAX_PATH],d[MAX_PATH];
- f->getfilename(a);
- f->getdir(d);
- if(strstr(a,name) && stricmp(d,dir)==0){
- char fullname[MAX_PATH];
- f->getfullname(fullname);
- file *ff2=new file(fullname,f->gettype());
- ff2->next=0;
- if(f1){
- f1->next=ff2;
- f1=ff2;
- }else{
- f1=ff2;
- head=f1;
- }
- }
- f=f->next;
- }
- }else{
- while(f){
- char a[MAX_PATH],d[MAX_PATH];
- f->getfilename(a);
- f->getdir(d);
- if(stricmp(a,name)==0 && stricmp(d,dir)==0){
- char fullname[MAX_PATH];
- f->getfullname(fullname);
- file *ff2=new file(fullname,f->gettype());
- ff2->next=0;
- if(f1){
- f1->next=ff2;
- f1=ff2;
- }else{
- f1=ff2;
- head=f1;
- }
- }
- f=f->next;
- }
- }
- }
- releasefile(f);
- }
- return head;
- }
- file* filesearch::subfiles(char *dir){
- file *f=subff(dir);
- file *head=0;
- file *f1=0;
- while(f){
- char name[MAX_PATH],d[MAX_PATH];
- f->getfullname(name);
- f->getdir(d);
- if(stricmp(dir,d)==0 && !(f->gettype() & FILE_ATTRIBUTE_DIRECTORY)){
- file *ff2=new file(name,f->gettype());
- ff2->next=0;
- if(f1){
- f1->next=ff2;
- f1=ff2;
- }else{
- f1=ff2;
- head=f1;
- }
- }
- f=f->next;
- }
- releasefile(f);
- return head;
- }
- file* filesearch::subfolders(char *dir){
- file *f=subff(dir);
- file *head=0;
- file *f1=0;
- while(f){
- char name[MAX_PATH],d[MAX_PATH];
- f->getfullname(name);
- f->getdir(d);
- if(stricmp(dir,d)==0 && (f->gettype() & FILE_ATTRIBUTE_DIRECTORY)){
- file *ff2=new file(name,f->gettype());
- ff2->next=0;
- if(f1){
- f1->next=ff2;
- f1=ff2;
- }else{
- f1=ff2;
- head=f1;
- }
- }
- f=f->next;
- }
- releasefile(f);
- return head;
- }
复制代码 给个实例:
main(){
filesearch f;
file* ff=f.subfolders("d:\\test");
file* head=ff;
while(ff){
char a[255];
ff->getfullname(a);
cout<<a<<endl;
ff=ff->next;
}
f.releasefile(head);
}
本帖来自:http://blog.csdn.net/sx1989827/archive/2010/08/25/5837119.aspx |
|