[C#] Skipping a folder based on the existence of a file

I’m having a brain-fart and need a nudge.

I’m scanning a folder structure for xml files, assuming one xml file per folder, and then processing based on the xml file. However I now need to change my working code to skip processing a folder based on the existence of a file that could have two possible names. I think part of my problem will be solved by using LINQ and lambdas

My working code is as follows:

foreach (string file in Directory.EnumerateFiles(Path, "*.xml", SearchOption.AllDirectories))
   {
      ... // Process the found xml files here
   }

I’m thinking of changing the code as follows:

var xmlfiles = Directory.EnumerateFiles(Path, "*.*", SearchOption.AllDirectories).Where(
                    f => !f.EndsWith("ignoreme.xml") && !f.EndsWith(".ignoreme")
                    );

Might need to add in a tolower there somewhere too.

The code is not quite right as it will just exclude those particular files from the results. What I want is to exclude the particular folder that the files exist in.

Ideally I want to try avoid scanning the folders more than once. I don’t have my heart set on LINQ, it doesn’t matter to me how this is done really. I’m just looking for a solution that works, and is not unnecessarily slow.

The following links contain interesting information, and may make some more sense when I’ve had some sleep:

Maybe something like this? I’m tired and obviously couldn’t test it out so maybe just try it out:

        List<string> allFiles = new List<string>();
        foreach (var directory in Directory.EnumerateDirectories(path, String.Empty, SearchOption.AllDirectories))
        {
            bool ignoreFolder = false;
            List<string> directoryFiles = new List<string>();

            foreach (var file in Directory.EnumerateFiles(directory, "*.*"))
            {
                if(file.EndsWith("ignoreme.xml", StringComparison.InvariantCultureIgnoreCase) || file.EndsWith(".ignoreme", StringComparison.InvariantCultureIgnoreCase))
                {
                    ignoreFolder = true;
                    break;
                }

                directoryFiles.Add(file);
            }

            if (!ignoreFolder)
                allFiles.AddRange(directoryFiles);
        }
1 Like

Thanks man, that’s enough to kick me in to gear. I’ll give it a whirl a bit later.

Don’t have a dev environment here to make sure it’s 100%, but something like this would also work:

var fileslist = from f in Directory.EnumerateFiles(Path,"*.*", SearchOption.AllDirectories)
                    where (!f.ToLower().EndsWith(“ignoreme.xml”)) && (!f.ToLower().EndsWith(".ignoreme"))
                    select f;

I would vote for GeForce’s solution, especially if you want to ignore the whole directory if it contains an “ignore” file. I think it is the easiest solution to exactly understand what it does. Vorty’s solution is ideal if you just want to ignore the “ignore” files.

My opinion, in general if unsure about the right processing for a situation, do not use LINQ. Overuse of LINQ is in my opinion one of the primary causes for a C# program to become unmaintainable/unreadable.

Thanks @GeForce, this helped me a lot. Needed a tweak here and there, but I have successfully implemented it and it is doing what it is supposed to do.

Big kisses. :heartbeat: :kissing_closed_eyes:

1 Like

:slight_smile: glad I could be of an assistance

You know, I could kick myself. I boosted the speed of this quite dramatically by doing something along these lines a few minutes ago:

    List<string> allFiles = new List<string>();
    foreach (var directory in Directory.EnumerateDirectories(path, "*", SearchOption.AllDirectories))
    {
       if (File.Exists(directory + @"\ignoreme.xml") || File.Exists(directory + @"\.ignoreme"))
         {
            continue;
         }
       List<string> directoryFiles = new List<string>();
	   foreach (var file in Directory.EnumerateFiles(directory, "*"))
         {
           directoryFiles.Add(file);
         }
       if (directoryFiles.Count > 0)
         {
           allFiles.AddRange(directoryFiles);
         }
    }

Yes, it is amazing the small little traps that can sap a program’s performance.