hi there
can any one write a C code that search for files in a directory
without using the windows apis(FindFirstFile() and FindNextFile())
or even has a small idea on how to do that.
thanks.
Yeah, I've read the responses here, but here is some actual code that
may or may not be what you're looking for:
/* read only certain types of entries in a directory to a list */
char **filter_directory_entry_list
(unsigned filter_type,char *filter,
unsigned entry_type,char *file_suffix,directory *directory) {
char **entry_item,**filtered_item;
if(directory==NULL) return NULL;
entry_item=filtered_item=directory->entry_list;
directory->num_filtered=0;
/* populate entry list with only filtered entries */
while(*entry_item!=NULL) {
if(filter_type==FILTERALPHA) {
if(!isalpha(*(*entry_item+1)))
goto Next_Entry;
}
else if(filter_type==FILTERNUMER) {
if(!isdigit(*(*entry_item+1)))
goto Next_Entry;
}
if(entry_type==DIRENTRIES) {
if(**entry_item!='d')
goto Next_Entry;
}
else if(entry_type==FILEENTRIES) {
if(**entry_item!='f')
goto Next_Entry;
if(file_suffix!=NULL) {
if(!is_file_type(*entry_item,file_suffix))
goto Next_Entry;
}
}
/* if you want all entries, pass NULL as filter */
if(filter==NULL)
goto Get_Item;
/* or match first few characters of entry */
else {
if(IS_STR_N_SAME(*entry_item+1,filter,strlen(filter)))
goto Get_Item;
else goto Next_Entry;
}
Get_Item :
*filtered_item++=*entry_item+1;
directory->num_filtered++;
Next_Entry :
entry_item++;
}
*filtered_item=NULL;
/* if we have two or more filtered entries, sort them alphabetically */
if(directory->num_filtered>1) {
/* temporarily strip suffix from file entries for proper sort */
if(entry_type!=DIRENTRIES) {
entry_item=directory->entry_list;
while(*entry_item!=NULL) {
if(*(*entry_item-1)=='f') {
*(strrchr(*entry_item,'.'))='\0';
entry_item++;
}
}
}
/* sort directory entries list to make sure it is in alpha order */
qsort(directory->entry_list,directory->num_filtered,
sizeof(char **),collate_alpha_strings_nocase);
/* replace suffix for file entries */
if(entry_type!=DIRENTRIES) {
entry_item=directory->entry_list;
while(*entry_item!=NULL) {
if(*(*entry_item-1)=='f') {
*(*entry_item+strlen(*entry_item))='.';
entry_item++;
}
}
}
}
return directory->entry_list;
}
What this does is allow you to find files that match certain criteria,
including, of course, the exact file you are looking for, by passing
the filename as *filter, and perhaps some other helpful restrictions,
such as appropriate values for filter_type, entry_type, and *file_suffix.
You have to pass it a *directory structure that was previously populated
by a function that implements your favorite directory reading functions
(I use POSIX-compliant functions as much as possible for that,
opendir(), readdir(), and closedir(), which are usually available in
most compilers).
Of course, I really use this to find lists of files in a directory with a
certain characteristic or name similarity, not find a single file...for
that,
probably the quickest way is to use the POSIX function access();
if you get back a "file not found" error, it ain't there, otherwise, it
is...