I have a parent folder that contains inside it multiple folders named Project-* (folders are numbered so Project-1, Project-15, Project-253 etc). Inside of each of those folders is a zip file, always the same name ‘project.zip’. This means that when the folder structure is correct it should ALWAYS be TopFolder\Project-*\project.zip.
I am trying to write a program that looks at each Project-* folder to see if the project.zip file is nested inside another folder (example: Project-*\newfolder\project.zip)
I have some code that looks for folders called Project-* and lists them if they have subdirectories, but it seems to consider my project.zip files as folders so it is showing EVERYthing as having subdirectories.
DirectoryInfo directory = new DirectoryInfo(txtbxOldFolder.Text);
DirectoryInfo[] folders = directory.GetDirectories("*Project-*", SearchOption.AllDirectories);
//finds folders nested in the folder
var query = from folder in folders
where folder.GetFileSystemInfos().Length > 0
select folder.FullName.ToString();
foreach (string str in query)
{
//this adds the path of any Project-* folder with subdirectories
listNestedFolder.Add(str);
}
As mentioned the above code does find any Project-* folder with subdirectories, however it also finds any that are Project-*\project.zip. How can I get it to stop identifying the zip file as a directory?
GetFileSystemInfos()returns both files and folders.You probably want to call
GetDirectories().For better performance, you can write
where folder.EnumerateDirectories().Any()