DDuffy
DDuffy

Reputation: 413

List all subdirs and files in directory

I was wondering if anyone knows of a cleaner way to do this. I have a section of my program that will list all input directories that contain files.

However, to do this I manually store each input directory as a variable and use these as targets for the search.

I was wondering if there is a cleaner way to do this?

For example, the pseudo code I have in my head at the moment is:

The deepest an input folder goes is 2 directories.

An example of the input folder structure is in the attached image.

enter image description here

An example of the code I currently use is as follows.

ListBox1.Items.Clear();

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "AustraliaFolder")).Length != 0) 
{ 
     ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "AustraliaFolder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "DE1Folder")).Length != 0) 
{
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "DE1Folder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "PL1Folder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "PL1Folder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "EuropeFolder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "EuropeFolder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "fr1Folder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "fr1Folder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "FranceFolder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "FranceFolder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "HKFolder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "HKFolder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "UKFolder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "UKFolder"))[0]); 
}

I have an inescapable tendency to over complicate things and was wondering if there was an easier, or at least more efficient way to do it than the pseudo code from my head.

Thank you.

Upvotes: 0

Views: 239

Answers (3)

Tejal Barot
Tejal Barot

Reputation: 80

var dirList = Directory.GetDirectories("Path", "*", SearchOption.AllDirectories).Where(subdir => !Directory.GetDirectories(subdir).Any());

It will return you all directory name which contains files

Upvotes: 0

sujith karivelil
sujith karivelil

Reputation: 29006

I suggest you to use a Dictionary<string, List<string>> to store the result, where the key fields stores the value of each directories and value fields stores the list of files in the corresponding directories. now take a look into the code:

Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
foreach (var item in Directory.GetDirectories(@"C:\Users\user\Desktop\search Directory", "*.*", SearchOption.AllDirectories))
{
    if (new DirectoryInfo(item).Name == "Input")
       dict.Add(item, getMyfiles(item));
}

This will loop through each directories and sub directories in the specified folder. in each iteration it calls the getMyfiles() which will return the files contained in the specified directory. We are collecting the directory name and the list of files in each iteration. Where the method getMyfiles() is defined like the following:

private List<string> getMyfiles(string DirectoryPath)
{
    return Directory.GetFiles(DirectoryPath, "*.*").ToList();
}

Upvotes: 1

Manfred Radlwimmer
Manfred Radlwimmer

Reputation: 13384

essentially i am looking for my listbox to be populated with the full path and filename of any files that exist within the folder "Input" or one of its subfolders. – DDuffy

All Files of a directory and it's subdirectories:

string[] files = Directory.GetFiles("Your Input Direcotry","*.*", SearchOption.AllDirectories);

This does improve on my original code. thank you. However, i would need to duplicate this for every input directory. I wouldn't be able to use this from the base directory, as each folder in the base directory contains both an input and an output folder. Would there be a way to iterate through each Input folder found within the base directory? – DDuffy

public static List<string> GetAllFilesOfAllDirectoriesCalledInput(string root)
{
    List<string> inputDirectories = FindSubDirectoriesCalledInput(root);
    List<string> result = new List<string>();
    foreach(string inputDirectory in inputDirectories)
        result.AddRange(Directory.GetFiles(inputDirectory,"*.*", SearchOption.AllDirectories));
    return result;
}

public static List<string> FindSubDirectoriesCalledInput(string currentRoot)
{ 
    List<string> results = new List<string>();
    foreach(string subDirectory in Directory.GetDirectories(currentRoot))
    {
        if(subDirectory.EndsWith("\\Input", StringComparison.InvariantCultureIgnoreCase))
            results.Add(subDirectory);  
        else
            results.AddRange(FindSubDirectoriesCalledInput(subDirectory));
     }
     return results;
 }

Upvotes: 1

Related Questions