65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Aug 13, 2002

1 min read

viewsIcon

49970

Process files based on a file filter and have the filter applied recursively to sub folders

Abstract

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

CDir * mydir = new CDir();
mydir->bRecursive = true;

mydir->dir ("c:\\*.*");
mydir->dir ("c:\\notes.txt");
mydir->dir ("c:\\*.doc");

Class Definition

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

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;
}