Main.cpp:
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<iostream>
#include<io.h>
#include<windows.h>
#include<time.h>
#include "IsMatch.h"
#define modelen 20
using namespace std;
int count = 0;
void FileSearch(char *path,char *mode)
{
char Tempath[MAX_PATH];
strcpy(Tempath,path);
strcat(Tempath,"*");
HANDLE hFind;
WIN32_FIND_DATA FileData;
hFind = FindFirstFile(Tempath,&FileData);
if( hFind == INVALID_HANDLE_VALUE )
{
printf("FindFirstFile failed (%d)\n", GetLastError());
return ;
}
else
{
do
{
if( (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& strcmp(FileData.cFileName,".")
&& strcmp(FileData.cFileName,"..") )
{
char Newpath[MAX_PATH];
strcpy(Newpath,path);
strcat(Newpath,FileData.cFileName);
strcat(Newpath,"\\");
FileSearch(Newpath,mode); }
else
{
char *lname;
_strlwr_s( lname = _strdup(FileData.cFileName), sizeof(FileData.cFileName));
if(match(mode,lname))
{
char cWholeName[MAX_PATH];
strcpy(cWholeName,path);
strcat(cWholeName,FileData.cFileName);
cout<<cWholeName<< "\t";
cout << FileData.nFileSizeLow << "-Bytes" << endl;
count ++;
}
free(lname);
}
}while(FindNextFile(hFind,&FileData));
FindClose(hFind);
}
}
void Function()
{
char Path[MAX_PATH];
char Mode[modelen];
char mode[modelen+2];
char *lmode;
int chose = 0;
part1: cout << "Input path:" << endl;
gets(Path);
if (Path[0] == NULL)
{
cout << "input error!" <<endl;
goto part1;
}
part2: cout << "Input file mode:" << endl;
gets(Mode);
if (Mode[0] == NULL)
{
cout << "input file mode error!" <<endl;
goto part2;
}
if(Path[strlen(Path)-1]!='\\')
{
strcat(Path,"\\");
}
strcpy(mode,"*");
strcat(mode,Mode);
strcat(mode,"*");
_strlwr_s( lmode = _strdup(mode), sizeof(mode));
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
FileSearch(Path,lmode);
cout << "File Number:" << count << endl;
count = 0;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
free(lmode);
}
int main()
{
while(true)
{
Function();
}
system("pause");
return 0;
}
IsMatch.h:
#include<iostream>
#include<string>
using namespace std;
bool match(char *pattern, char *content) {
if ('\0' == *pattern && '\0' == *content)
return true;
if ('*' == *pattern && '\0' != *(pattern + 1) && '\0' == *content)
return false;
if ('?' == *pattern || *pattern == *content)
return match(pattern + 1, content + 1);
if ('*' == *pattern)
return match(pattern + 1, content) || match(pattern, content + 1);
return false;
[edit]Code block added - OriginalGriff[/edit]