Click here to Skip to main content
12,898,500 members (65,807 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 12 Aug 2002

File filtering and directory recursion in .NET using Managed Extensions for C++

, 12 Aug 2002
Rate this:
Please Sign up or sign in to vote.
Process files based on a file filter and have the filter applied recursively to sub folders


VB has a cool Dir command that lets you work with files and file attributes fairly easy. Unfortunately, this command doesn't exist in .NET; so, to get the same functionality we have to create our own. This article covers a simple object CDir that allows for file filtering using wildcards and directory recursion.

How it works

The dir method takes in a string parameter containing the directory and filter and then separates them into the two components. The filter component is then used to create a collection of files matching the filter, each item in the collection calls the ProcessFile method.

If recursion is enabled, a collection of folders in the current directory is created. Each item in the folder collection calls back the dir method with the path and filter to process its files. This continues until all the files get processed.

To give you an example of how it's implemented, the following code instantiates an object and sets the recursion on. The filter passed to the dir method begins the file processing. The code processing the files is in the ProcessFile method. Currently it just outputs the full file name to the console.

Implementing the object

<pre lang=mc++> CDir * mydir = new CDir(); mydir->bRecursive = true; mydir->dir ("c:\\*.*"); mydir->dir ("c:\\notes.txt"); mydir->dir ("c:\\*.doc");

Class Definition

<pre lang=mc++> using namespace System; using namespace System::IO ; __gc class CDir { private: virtual void ProcessFile (FileSystemInfo * myFile); String * GetDirectoryPath (String * myPath); String * GetFileFilter (String * myPath); public: bool bRecursive; CDir (); void dir (String * myPath); };

Class Implementation

<pre lang=mc++> CDir::CDir() { bRecursive=false; } void CDir::ProcessFile (FileSystemInfo * myFile) { Console::WriteLine (myFile->FullName); } void CDir::dir (String * myPath) { FileSystemInfo * myFile,* myFolder; FileSystemInfo * myFileCollection[], * myFolderCollection[]; DirectoryInfo * myDirectory; String * DirectoryPath; String * Filter; int index ; try { DirectoryPath = GetDirectoryPath (myPath); Filter = GetFileFilter(myPath); myDirectory = new DirectoryInfo (DirectoryPath); myFileCollection = myDirectory->GetFiles (Filter); for (index=0;index < myFileCollection->Count ;index++) { myFile = myFileCollection[index]; ProcessFile (myFile); } // If we are going recursive, look for other directories // under the current folder if (bRecursive) { myFolderCollection = myDirectory->GetDirectories (); for (index=0;index < myFolderCollection->Count ;index++) { myFolder = myFolderCollection[index]; dir (String::Concat (myFolder->FullName,"\\",Filter)); } } } catch (Exception * e) { Console::WriteLine (e->Message); } } String * CDir::GetDirectoryPath (String * myPath) { int pos =0; int OldPos=0; String * Directory; while ((pos = myPath->IndexOf ("\\",pos))>0) { OldPos= pos; pos++; } if (OldPos==0) Directory = String::Concat (System::Environment::CurrentDirectory,"\\"); else Directory = myPath->Substring (0 , OldPos+1); return Directory; } String * CDir::GetFileFilter (String * myPath) { int pos =0; int OldPos=0; String * Filter; while ((pos = myPath->IndexOf ("\\",pos))>0) { OldPos= pos; pos++; } if (OldPos == 0) Filter = myPath; else Filter = myPath->Substring (OldPos+1,myPath->Length-(OldPos+1) ); return Filter; }


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


About the Author

Bill Ferreira
Web Developer
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 13 Aug 2002
Article Copyright 2002 by Bill Ferreira
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid