0

So here is the folder I wish to select: Selected Folder using FBD

Within that BackPath folder are 7 other dated folders as shown: Dated Folders within selected folder

Within each of those dated folder are two more folders, named "In" and "Out".

What I want to do is just select the BackPath folder and then when I run my import method I want it to search through each of the dated folders and only look at the "Out" folders in each and ignore each "In" folder.

Here is my chooseInputFolder method:

private void ChooseInputFolder()
    {
        try
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "Please Select An Input Path";
            fbd.SelectedPath = @"C:\";

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                tbInputPath.Text = fbd.SelectedPath;
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error Occured While Trying To Populate Combo Boxes : " + ex.Message);
        }
    }

Here is my foreach loop that looks in the selected directory:

foreach (var files in Directory.GetFiles(strPath))

3 Answers3

0

So I managed to resolve this by implementing the following code:

foreach (string subDir1 in Directory.GetDirectories(strPath))
                {
                    foreach (string subDir2 in Directory.GetDirectories(subDir1))
                    {
                        foreach (var files in Directory.GetFiles(subDir2))
                        {
                            if(subDir2.Contains("In"))
                            {
                                //IMPORT FILES METHOD GOES HERE
                            }
                            else
                            {
                                continue;
                            }

                        }
                    }

                }
  • to do this make a method which take in a list of directory... then in that method you loop and then pass it to the method you just called – Seabizkit Apr 16 '20 at 12:20
0

sudo code so you get the idea.....

the benefit of this code, is the depth of the folders no longer matter.

if your file was 9 folders deep that would be a lot of for-each blocks. this code doesn't need any changes regardless of depth

Start()
{
    var directories= SomeList(path);
    Search(directories)
}

Search(List<T> directorys)
{
    foreach(var item in directorys)
    {
        //some if conditions to end early if you find what you looking for.         
        //or logic to maintain tracking of sorts.

        //next set
         var folders = GetFolder(item);
         Search(folders)
    }
}
Seabizkit
  • 2,417
  • 2
  • 15
  • 32
0

As others suggested, you can use recursion to simplify your code and get the folder you wanted.

I make a code example and you can have a look.

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           folderBrowserDialog1.SelectedPath = @"D:\";
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
            DirSearch(textBox1.Text);
        }


        public void DirSearch(string sDir)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    foreach (string f in Directory.GetDirectories(d))
                    {
                        if(f.Contains("In"))
                        {
                            listBox1.Items.Add(f);
                        }

                    }
                    DirSearch(d);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }
    }

The result:

enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27