1

I'm trying to check whether a directory is in a format of

Ex:

This should be valid: D:\TESTDIR\FOLDER1\FOLDER2\Somerandomfiles

This is not valid: D:\TESTDIR\FOLDER1\Somerandomfiles

Basically I need to make sure that for every folder there should be a subfolder inside. note that a single folder may contain multiple subfolder. Then the subfolder will contain the files such as text files/pdf files etc.

   //not sure how/where to place to check if folder has a subfolder 
   var _dir = Directory.GetDirectories(@"D:\TESTDIR");

   foreach(var _folder1 in _dir)
   {
       // check if contains folder2? if not 
       // MessageBox.Show(folder1name);
       var _folder2 = Directory.GetDirectories(_folder1);
            
       foreach (var _path in _folder2)
       {
           // do something
       }
   }
Jane Ortega
  • 87
  • 1
  • 8
  • If it were me, I would add the search option to not get subdirectories. https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getdirectories?view=net-5.0#System_IO_Directory_GetDirectories_System_String_System_String_System_IO_SearchOption_ . You could then know what level each folder is at more easily. You might also consider splitting the path string on the directory separator. https://stackoverflow.com/questions/401304/how-does-one-extract-each-folder-name-from-a-path – David.Warwick Aug 06 '21 at 01:32
  • I mean not get subdirectories from Directory.GetDirectories – David.Warwick Aug 06 '21 at 01:40
  • I suspect it's because you're not overly clear. Could you express "note that folder2 may be multiple in 1 folder1" more clearly? – Enigmativity Aug 06 '21 at 06:24
  • meaning that inside folder1 may contains many subfolders (folder2) should have have mentioned subfolders instead – Jane Ortega Aug 06 '21 at 06:26
  • edited hopefully its clear/better thank you – Jane Ortega Aug 06 '21 at 06:30

2 Answers2

1

You almost got it right, I just modified your code a little:

var dir = Directory.GetDirectories(@"D:\TESTDIR");

foreach(var folder1 in dir)
{
    var folder2 = Directory.GetDirectories(folder1);
    // If no directories are found in that subfolder, skip it.
    // Basically it would work the same without if, as loop
    // would not run even one iteration, but it's more readable.
    if(folder2.Count == 0) continue;
    
    foreach (var path in folder2)
    {
        var files = Directory.GetFiles(path);
        // Same as above, check if there are any files.
        if(files.Count == 0) break;
        // Do some work
    }
}
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

Here's how I would start this:

var query =
    from dir in Directory.GetDirectories(@"D:\TESTDIR")
    select new
    {
        dir,
        valid =
            Directory.GetDirectories(dir).Any()
            && Directory.GetDirectories(dir).All(child => Directory.GetFiles(child).Any())
    };

That's going to check each of the directories in @"D:\TESTDIR" and ensure each has at least one subfolder and that each subfolder has at least one file.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172