#include <acknex.h>
#include <windows.h>
typedef struct
{
char* filename[MAX_PATH];
}my_files;
int total_found=0;
//char* findthis : search criteria
//char* skipthischar :exclude filenames with an occurance of this caracter/s
//int atpos :the occurance of the caracter must be at this position ,to skip the filename
// for example, skipthischar="_" , atposition=1 in string "_example.exe" wil be skipped ,but "ex_ample" not
// if atpos=0 ,then the filename is skipped no matter where the position is at eg:"ex_ample" will be skipped
//int max_filenames :max filename's array size
my_files* get_filenames(char* findthis,char* skipthischar,int atpos,int max_filenames)
{
if(atpos<1) atpos=0;
if(atpos>MAX_PATH)atpos=MAX_PATH;
int current=0;
my_files *filenames=malloc(sizeof(my_files)*max_filenames);
WIN32_FIND_DATA find_result;
char searchfor[MAX_PATH];
HANDLE find_handle=INVALID_HANDLE_VALUE;
DWORD error=0;
memcpy(searchfor,findthis,MAX_PATH);
find_handle=FindFirstFile(searchfor,&find_result);
if(find_handle==INVALID_HANDLE_VALUE)
{
printf("none found");
FindClose(find_handle);
return;
}
//first found
else
{
if(atpos>0)
{
if(str_stri(_chr(find_result.cFileName),skipthischar)!=atpos)
memcpy(filenames[0].filename,_chr(find_result.cFileName),MAX_PATH);
}
else if(atpos==0)
{
if(str_stri(_chr(find_result.cFileName),skipthischar)==atpos)
memcpy(filenames[0].filename,_chr(find_result.cFileName),MAX_PATH);
}
}
//find others
while(FindNextFile(find_handle,&find_result)!=0)
{
error=GetLastError();
if(error!=ERROR_NO_MORE_FILES)
{
if(atpos>0)
{
if(str_stri(_chr(find_result.cFileName),skipthischar)!=atpos)
{
if(current+1<max_filenames)
{
current+=1;
memcpy(filenames[current].filename,_chr(find_result.cFileName),MAX_PATH);
}
}
}
else if(atpos==0)
{
if(str_stri(_chr(find_result.cFileName),skipthischar)==atpos)
{
if(current+1<max_filenames)
{
current+=1;
memcpy(filenames[current].filename,_chr(find_result.cFileName),MAX_PATH);
}
}
}
}
}
FindClose(find_handle);
total_found=current+1;
return filenames;
}
void main()
{
//find *.mdl but skip all that has "_" at position 1
my_files *filenames=get_filenames("*.mdl","_",1,100);
//or
//find *.mdl but skip all that has "_" at any position
//my_files *filenames=get_filenames("*.mdl","_",0,100);
if(filenames!=NULL)
{
int i;
for(i=0;i<total_found;i++)
{
printf(_chr(filenames[i]));
}
sys_free(filenames);
}
}