Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / MFC

Recursive patterned File Globbing

Rate me:
Please Sign up or sign in to vote.
4.93/5 (20 votes)
27 Aug 20029 min read 186.2K   2K   61  
Class and application to recursively or non-recursively match files or directories based on a wildcard pattern.
#include "FileGlobBase.h"
#include "FileGlobList.h"

/**
	A specialized derived class that just prints the filenames to stdout.
**/
class FileGlobPrint : public FileGlobBase
{
	virtual void FoundMatch( const char* fileName )
	{
		printf( "%s\n", fileName );
	}
};


void Usage()
{
	printf( "Glob - A file globbing utility\n" );
	printf( "    Algorithm and original implementation by Matthias Wandel (MWandel@rim.net)\n" );
	printf( "    Extensions and C++ interface by Joshua Jensen (jjensen@workspacewhiz.com)\n" );
	printf( "\nUsage: Glob -i pattern patterns\n" );
	printf( "   -e pattern = Exclusive pattern.  All ignore patterns are not used.  Only\n" );
	printf( "                files matching the exclusive pattern are counted." );
	printf( "   -i pattern = Ignore patterns of the name [pattern].  Close with a\n" );
	printf( "                forward slash to ignore a directory." );
	exit( -1 );
}


int main (int argc, char **argv)
{
//	FileGlobList glob;
	FileGlobPrint glob;

	if ( argc == 1 )
		Usage();

	for ( int argn = 1; argn < argc; ++argn )
	{
		char* arg = argv[ argn ];
		if ( arg[0] != '-' )
			break; // Filenames from here on.

		if ( strcmp( arg, "-e" ) == 0 )
		{
			argn++;
			glob.AddExclusivePattern( argv[ argn ] );
		}
		else if ( strcmp( arg, "-i" ) == 0 )
		{
			argn++;
			glob.AddIgnorePattern( argv[ argn ] );
		}
		else if ( strcmp( arg, "-?" ) == 0 )
		{
			Usage();
		}
	}
	
	for ( ; argn < argc; ++argn)
		glob.MatchPattern( argv[argn] );

#if 0
	for ( FileGlobList::Iterator it = glob.begin(); it != glob.end(); ++it )
	{
		const char* str = (*it).c_str();
		printf( "%s\n", str );
	}
#endif 0

	return EXIT_SUCCESS;
}

/*

s:\c*\*
s:\c*\*\
s:\c*\...
s:\c*\...\
\c*\*
*\*.c
...\*.c
/*
/...
..\...

*/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Joshua Jensen is a gamer at heart and as such, creates games for a living. He has the distinct pleasure of creating titles exclusively for the Xbox.

In his spare time, he maintains a Visual C++ add-in called Workspace Whiz! Find it at http://workspacewhiz.com/.

Comments and Discussions