Click here to Skip to main content
Click here to Skip to main content

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

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

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

Share

About the Author

Bill Ferreira
Web Developer
Canada Canada
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 13 Aug 2002
Article Copyright 2002 by Bill Ferreira
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid