Click here to Skip to main content
15,881,380 members
Articles / Desktop Programming / ATL

File and Directory Enumeration

Rate me:
Please Sign up or sign in to vote.
4.61/5 (15 votes)
3 Mar 2003Ms-PL4 min read 203.9K   4.6K   62  
Template based file and directory enumeration class.
#include <windows.h>
#include <tchar.h>
#include <conIO.H>

#pragma warning(push, 2)
#include <string>
#include <VECTOR>
using std::string;
using std::vector;
#pragma warning(pop)

#include "RecurseDir.h"

//////////////////////////////////////////////////////////////////////////
//
// derived class which implements a filter
//
class CTest : public CDirectoryContent
{
public:
	explicit CTest(LPCTSTR pFilter) : m_filter(pFilter)
	{
	}

	virtual bool CheckUseFile (LPCTSTR , WIN32_FIND_DATA* pwfd ) 
	{
		return ::PathMatchSpec(pwfd->cFileName, m_filter.c_str()) != FALSE;
	}

	void dump()
	{
		for(arraytype::iterator i = List().begin(); i != List().end(); ++i)
			cprintf(_T("%s\t%i bytes\n"), (*i).cFileName, (*i).nFileSizeLow);
	}

private:
	std::string m_filter;
};



int main(int argc, char* argv[])
{
	if(argc == 2)
	{
		TCHAR	szCurrentDir[MAX_PATH];
		::GetCurrentDirectory(MAX_PATH, &szCurrentDir[0]);

		CTest	dir(argv[1]);

		if(dir.Run(szCurrentDir))
			dir.dump();
		else
			cputs(_T("Run() failed"));

	}
	else
	{
		cputs(_T("usage: RecurseDirText.exe filespec\n e.g.: RecurseDirTest.exe *.bmp"));
	}

	//
	// uncomment this to recursively delete the directory given as parameter 
	//
	/*
	
	  CCleanDir rd;
	  rd.Run(argv[1]);

	*/

	return 0;
}

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, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Portugal Portugal
Software Smith, Blacksmith, Repeat Founder, Austrian, Asgardian.

Comments and Discussions