1

While trying to complete a duplicate file software I noticed there was an issue with the Directory.GetDirectories() method. When the folder was not accessible for whatever reason (most of the time it was due to the folder being a system folder) the loop would stop since an exception would be thrown.

After some research trying to figure out what would be the best method to do the recursion manually I found Marc Gravell's example which worked perfectly for my needs.

My issue with the above method is that I can't figure out how it will be possible to know when the recursion method has finished processing any files/folders. So any insights on the matter would be appreciated.

Thanks in advance.

Community
  • 1
  • 1
denied66
  • 644
  • 7
  • 18
  • Did you want a progress bar? You could have a member variable called folderDepth and increment/decrement as the method traverses down the folder hierarchy. Put in `Debug.Write(folderDepth)` before you run the method and you'll get *an idea* of you could better predict when it will finish. Just my 2 cents:) Keen to hear some answers on this now +1 – Jeremy Thompson Jun 30 '12 at 17:51
  • At the moment the main concern is just finding out when the searching is complete to output a message to the user. Your suggestion for a variable implies that I need to know how many folders are already in selected folder, which isn't possible, at least not with using something like Directory.GetDirectories().Length – denied66 Jun 30 '12 at 17:54

2 Answers2

1

Put a message box after the operation comepletes:

static void Main()
{
  string path = ""; // TODO
  ApplyAllFiles(path, ProcessFile, FolderProcessed);
  MessageBox.Show("Operation complete.");
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Well this is awkward... It seems my brain stopped functioning properly for a reason, I kept thinking that the thread wasn't blocked while the recursion was happening. Thanks for the help. – denied66 Jun 30 '12 at 18:04
  • Glad to have helped, one last note since you mentioned threads - please have a look at some of the links in this answer: http://stackoverflow.com/a/10965388/495455 – Jeremy Thompson Jun 30 '12 at 18:07
  • Thanks for clarifying your comment on my question, but I still can't see how this can be used to show the process using a process bar. Don't you need to know what the result should be in order to illustrate the process somewhere ? The current design seems like it doesn't offer that. With Marc Gravell's example it doesn't seem possible to know how many files you need to recurse over and by using Directory.GetDirectories() you are risking getting an exception. Thankfully the program doesn't involve illustrating the progress since that seems like a hard thing to do under these circumstances. – denied66 Jun 30 '12 at 18:13
  • yeah crazy wild idea, that would lead to the likelyhood of increasing the time to do an operation - sound familiar? Although its the way [many](http://utorrent.com) applications resort to give basic estimates. – Jeremy Thompson Jun 30 '12 at 18:19
  • The link you provided above seems useful for this scenario. Using LINQ seems to allow you to iterate over the items without the need to wait for it to process all the files and of course at end you can check for how many items your array contains. Shame it's requires .net 4.0 as that's a pretty big dependency for "casual" pc users. – denied66 Jun 30 '12 at 18:25
0

Can you clarify what you're trying to do?

In the example code provided by Marc, you must provide an Action that will be called for every file discovered, thus the invoking of that action represents the processing of that file.

If you're looking for a way to determine when all of the files and subfolders within a folder have been processed, you'd need to modify the code to add a second action. Something like this:

using System;
using System.IO;
static class Program
{
    static void Main()
    {
        string path = ""; // TODO
        ApplyAllFiles(path, ProcessFile, FolderProcessed);
    }
    static void ProcessFile(string path) {/* ... */}
    static void FolderProcessed(string path) {/* ... */}

    static void ApplyAllFiles(string folder, Action<string> fileAction, Action<string> folderProcessed)
    {
        foreach (string file in Directory.GetFiles(folder))
        {
            fileAction(file);
        }
        foreach (string subDir in Directory.GetDirectories(folder))
        {
            try
            {
                ApplyAllFiles(subDir, fileAction);
            }
            catch
            {
                // swallow, log, whatever
            }
        }

        //**** New action invoked here:
        folderProcessed(folder);
    }
}
John M. Wright
  • 4,477
  • 1
  • 43
  • 61
  • The issue here is that I need to know when all files or folders have been processed. Based on what you are saying it means I already have an array with those objects which I can check to verify that everything has been processed, which is the issue since I can't use any method for getting the number of folders/files without the risk that one of them might be a folder that throws an exception. – denied66 Jun 30 '12 at 17:57