Bumba
Bumba

Reputation: 333

Get files from specifically structured sub-folders?

How do I get *.xml files from a specifically structured folder/sub-folder system in array to perform some operation.

Eg: The sample structure of the parent folders in user provided path (say, myPath) is

2017-36459-20124-301236\2017\36459\20124\301236\301236.xml

I cannot use things like string[] tarDir = Directory.GetDirectories(myPath, "foldernameinitial"); as the folder name is changeable. Does anyone have any idea how to solve this issue?

Upvotes: 0

Views: 185

Answers (2)

Tamal Banerjee
Tamal Banerjee

Reputation: 503

 var job_folders = Directory.EnumerateDirectories(textBox1.Text, "*", SearchOption.TopDirectoryOnly);
            if (job_folders.ToArray().Length == 0)
            {
                MessageBox.Show("NO job folders are found...");
            }
            else
            {
                foreach (string job_folder in job_folders)
                {
                    var target_xml_file = Directory.GetFiles(job_folder, "*.xml", SearchOption.AllDirectories).Where(x => Path.GetFileName(Path.GetDirectoryName(x)).ToLower() == "xml");
                    var target_meta_file = Directory.GetFiles(job_folder, "*.xml", SearchOption.AllDirectories).Where(x => Path.GetFileName(Path.GetDirectoryName(x)).ToLower() == "meta");

                }
            }

Upvotes: 0

boop_the_snoot
boop_the_snoot

Reputation: 3247

As I gathered clarification from your comments, this will get you all the sub-directories with only files in them ie., the last sub-directory

static IEnumerable<string> GetLastDirectory(string path) =>
     Directory.GetDirectories(path, "*", SearchOption.AllDirectories)
       .Where(dir => Directory.GetDirectories(dir).Length == 0);

Now use it as:

var MyDirectories = GetLastDirectory(@"D:\Softwares\Xtras"); //your path goes here
foreach (var subdir in MyDirectories)
{
   var onlyXMLfiles = Directory.GetFiles(subdir, "*.xml");
   foreach (var file in onlyXMLfiles)
   {
      //do your operation
   }
}

To be frank I don't know regex, I tried this pattern match at regex101. But as you said in the comments below you want to match the pattern of directory structure also, you can do this:

string pattern = @"\d{4}-\d{4,10}-\d{4,10}-\d{4,10}\\\d{4}\\\d{4,10}\\\d{4,10}\\\d{4,10}";

//Now you won't have to use "GetLastDirectory", instead use "Directory.GetDirectories"
var MyDirectories = Directory.GetDirectories("your path goes here");
foreach (var subdir in MyDirectories)
{
    if ((Regex.Match(subdir, pattern) != Match.Empty))
    {
       var onlyXMLfiles = Directory.GetFiles(subdir, "*.xml");
       foreach (var file in onlyXMLfiles)
       {
           //do your operations
       }
    }         
}

Probable pattern explanation:

\        :   match keyword, maybe!?<br>
-        :   hyphen as mentioned in the folder structure<br>
\d       :   match digits only<br>
\d{4}    :   match digits of length 4 and above<br>
\d{4,10} :   match digits of length 4 and limit upto upto 10<br>
\\       :   match \ as in the folder path<br> 

Upvotes: 1

Related Questions