Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Main.cpp:
C++
#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&lt;&lt;cWholeName&lt;&lt; "\t";
					cout &lt;&lt; FileData.nFileSizeLow &lt;&lt; "-Bytes" &lt;&lt; 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;
	//input path("eg:c:\...\")
	part1: cout &lt;&lt; "Input path:" &lt;&lt; endl;
	gets(Path);
	if (Path[0] == NULL)
	{
		cout &lt;&lt; "input error!" &lt;&lt;endl;
		goto part1;
	}
	//input file mode (eg:*.txt or  *.exe)
	part2: cout &lt;&lt; "Input file mode:" &lt;&lt; endl;
	gets(Mode);
	if (Mode[0] == NULL)
	{
		cout &lt;&lt; "input file mode error!" &lt;&lt;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 &lt;&lt; "~~~~~~~~~~~~~~~~~~~~~~~~~~" &lt;&lt; endl;
	FileSearch(Path,lmode);
	cout &lt;&lt; "File Number:" &lt;&lt; count &lt;&lt; endl;
	count = 0;
	cout &lt;&lt; "~~~~~~~~~~~~~~~~~~~~~~~~~~" &lt;&lt; endl;
	free(lmode);
}	
int main()
{
	while(true)
	{
		Function();
	}
	system("pause");
	return 0;
}


IsMatch.h:
C++
#include<iostream>  
#include<string>  

using namespace std; 

bool match(char *pattern, char *content) { 
     //if we reatch both end of two string, we are done  
    if ('\0' == *pattern && '\0' == *content) 
        return true; 
    /* make sure that the characters after '*' are present in second string.
      this function assumes that the first string will not contain two
       consecutive '*'*/ 
    if ('*' == *pattern && '\0' != *(pattern + 1) && '\0' == *content) 
        return false; 
    // if the first string contains '?', or current characters of both   
    // strings match  
    if ('?' == *pattern || *pattern == *content) 
        return match(pattern + 1, content + 1); 
    /* if there is *, then there are two possibilities
       a) We consider current character of second string
       b) We ignore current character of second string.*/ 
    if ('*' == *pattern) 
        return match(pattern + 1, content) || match(pattern, content + 1); 
    return false; 


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 12-Aug-14 22:40pm
v3
Comments
OriginalGriff 12-Aug-14 14:23pm    
Depends on the environment your code is working in: if it's native it's one set of solutions, if it's C++/CLR then it's another, and so forth.
DingguoHong 13-Aug-14 3:58am    
I'll show you my code,please help me check it .
DingguoHong 13-Aug-14 4:41am    
this is my code
Sergey Alexandrovich Kryukov 12-Aug-14 15:08pm    
It also depends on OS, which you need to tag with your question. If you need multiplatform solution, you need to indicate that, then you can find some library providing OS abstraction, and then you can just read original documentation.
—SA
DingguoHong 13-Aug-14 3:59am    
I'll send you my code ,please help me check it .Thank you!

1 solution

Replacing strcat(Tempath,"*") with strcat(Tempath, "*.exe") would do the job but I suspect you know that.

This is a good opportunity to encourage you to create a couple of units tests. The idea here is to isolate areas of code. Bugs observed when testing only that bit of code can only come from that code.

First, validate your FindFirstFile/FindNextFile logic. Replace the match function with one that always returns true. Your test will be to see if the list of files matches a shell command like "dir/s".

Second, to validate the match function, write a separate main() function that calls it in a loop with different combinations of pattern strings and file name strings.

Third, run the two together after any needed corrections.

A few code observations:

IsMatch.h is missing a closing '}' brace. Also, the #include's don't belong there. Move them to the main source file.

Any functions that are local file scope only should be static (eg. "FileSearch" and "Function").

Your match function looks good though I have not run it. It's recursive so the stack will get deep with long file names. This looks like an academic exercise so that's probably okay.

I'm not sure what the match function would do with two consecutive asterisks '*'. This would be an error. It's up to you if you want to guard against that.

Notice that my answer isn't trying to run or debug your code for you but give you the tools to do this for yourself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900