Click here to Skip to main content
15,905,563 members
Articles / Programming Languages / C++/CLI
Article

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
12 Aug 20021 min read 49.9K   16  
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

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

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

Class Definition

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

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

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
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --