Rss Feed
Tweeter button
Facebook button
Technorati button
Reddit button
Myspace button
Linkedin button
Webonews button
Delicious button
Digg button
Flickr button
Stumbleupon button
Newsvine button
logo

Recursively search a directory to obtain a file list (Handles locked folders)

logo

When using the GetFiles() C# method, we often have the problem of the search ending the moment it reaches a locked file or folder. Here we workaround that by writing our own recursive search method which is able to skip locked files and folders.

/// <summary>
/// Process a valid file, add it to the list of filepaths
/// </summary>
/// <param name="path"></param>
private void ProcessFile(string path)
{
    filePaths.Add(path);
}

/// <summary>
/// Recursively search the directory and when a valid file is found, do something with it
/// </summary>
/// <param name="folder">folder to process</param>
/// <param name="fileAction">delgate to do something to the filepath</param>
private static void ApplyAllFiles(string folder, Action<string> fileAction)
{
    foreach (string file in Directory.GetFiles(folder))
    {
        fileAction(file);
    }
    foreach (string subDir in Directory.GetDirectories(folder))
    {
        try
        {
            ApplyAllFiles(subDir, fileAction);
        }
        catch
        {
            //Some file types are locked and can't be processed, swallow the error
        }
    }
}

 

The above is the recursive function using a delegate to call ProcessFile() for each file we find. In this case, we swallow/skip any errors (locked folders) in the catch statement. We add each file found to a filePaths list.

private List<string> filePaths;

 

So to call the recursive search function, just use:

ApplyAllFiles(folder, ProcessFile);

Whereby, folder is the string of the path of the folder to search in.

Here, we’ve done our recursive search and stored all valid files into the filePaths list. This runs through every subfolder and file that is not inaccessible and you can add your own processing to the method.

11 Responses to “Recursively search a directory to obtain a file list (Handles locked folders)”

  1. Generally speaking, swallowing exceptions like this is a *Bad Thing*. What if an exception occurs that means the subsequent processing could becausing damaged to the file system, the code will just carry on grinding through the file system. The advice from Brad Abrams etc in the Framework Design Guidelines, 2nd edition is (broadly speaking): “Avoid the temptation to catch everything”. Mind you I guess if the actual per-file IOs you’re executing are read only then you reduce the chance of things going badly wrong in the event of errors.
    – Tom

  2. admin says:

    Yes, thats very true. I think its generally preferable to log such errors since i can’t think of a way around that.

  3. CAMERON says:

    Medicamentspot.com International Legal RX Medications. Special Internet Prices (up to 40% off average US price). NO PRIOR PRESCRIPTION REQUIRED!…

    Combivir@buy.online” rel=”nofollow”>.…

  4. ZACHARY says:


    Medicamentspot.com. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. Low price drugs. Buy drugs online

    Buy:Actos.Human Growth Hormone.Nexium.Accutane.Mega Hoodia.Lumigan.Arimidex.Valtrex.Prednisolone.Synthroid.Retin-A.Zovirax.100% Pure Okinawan Coral Calcium.Petcam (Metacam) Oral Suspension.Zyban.Prevacid….

  5. JUAN says:

    Accupril

    Buygeneric drugs…

  6. GABRIEL says:

    Abilify

    Buygeneric pills…

  7. LAWRENCE says:

    Buygeneric meds…

  8. JOHN says:

    shimmy@adalat.simone” rel=”nofollow”>..

    Buyit now…

  9. BRIAN says:

    buy@cheap.viagra.in.uk” rel=”nofollow”>.

    Buywithout prescription…

  10. ROLAND says:

    prozac@dangers.now” rel=”nofollow”>..

    Buygeneric drugs…

Leave a Reply

You must be logged in to post a comment.

logo
logo
Powered by Wordpress