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:
Date
, If FolderName == "Input"
then store all subfolders in array InputSubs
InputSubs
, if no subfolder is contained within InputSub
and no files are contained in InputSubs
then move on to nextInputSubs
contains a subdir, then store all subdirs in array InputSubs2
and move on to nextInputSubs
contains files, store DirPath
and FileName
as string in array InputFiles
InputSubs
process for InputSubs2
The deepest an input folder goes is 2 directories.
An example of the input folder structure is in the attached image.
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.
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;
}