• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

get all the folder names in a specific folder into a string array

I have a fax folder, under which there are several folders for each faxline. I need to process all the  files in each of the faxline folder in the fax folder and write processed history in a table which would include faxline folder name and file name with processed flag. For this i need to put all my faxline folders in a string array and then process each file in each folder in a loop.

How do I get all the folder names in the fax folder in a string array?

Thanks,
0
patd1
Asked:
patd1
3 Solutions
 
Evan CutlerCommented:
using System;
using System.IO;

class Program
{
    static void Main()
    {
      // Put all file names in root directory into array.
      string[] array1 = Directory.GetFiles(@"C:\");

      // Put all txt files in root directory into array.
      string[] array2 = Directory.GetFiles(@"C:\", "*.BIN"); // <-- Case-insensitive

      // Display all files.
      Console.WriteLine("--- Files: ---");
      foreach (string name in array1)
      {
          Console.WriteLine(name);
      }

      // Display all BIN files.
      Console.WriteLine("--- BIN Files: ---");
      foreach (string name in array2)
      {
          Console.WriteLine(name);
      }
    }
}

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
First use GetDirectories(), then use GetFiles() as suggested by arcee123:
string FaxPath = @"c:\some\folder";
            foreach (string Folder in System.IO.Directory.GetDirectories(FaxPath)) 
            {
                foreach (string File in System.IO.Directory.GetFiles(Folder))
                {
                    Console.WriteLine(File);
                }
            }

Open in new window

0
 
anarki_jimbelCommented:
OK, the question was to create an array.
Pls see my variation (partly copied from Idle_Mind, always a good place to copy from :))
private void button3_Click(object sender, EventArgs e)
        {
            string FaxPath = @"c:\work\FaxFolder";
            List<string> FaxLinesList = new List<string>();
            foreach (string Folder in System.IO.Directory.GetDirectories(FaxPath))
            {
                FaxLinesList.Add(Folder);

            }
            // get an array if you really need an array but not a list
            string[] FaxLinesArray = FaxLinesList.ToArray();
            // print our folder names
            System.Diagnostics.Debug.WriteLine("There are " + FaxLinesArray.Length + " FixLine folders");
            foreach (string FaxLineFolderName in FaxLinesArray)
            {
                System.Diagnostics.Debug.WriteLine(FaxLineFolderName);
                //Console.WriteLine(File);
            }
        }

Open in new window

0
 
patd1Author Commented:
Great! Thanks.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now