Reputation: 1259
I have folder structure like follow
Path of the main folder is
C:\Users\me\Desktop\main_folder\
withing that there can be any number of sub folders with any name.
what i need to do is go in to each of that sub folder and extract the zip files in it.
For this, in powerhsell we can use the path like follow which takes any sub folder in the given folder.
C:\Users\me\Desktop\main_folder\*\*.zip
but how to do this in C# ?
method that i'm trying to use is
System.IO.Directory.GetFiles("C:\Users\me\Desktop\main_folder\*\",
"*.zip",System.IO.SearchOption.TopDirectoryOnly);
Upvotes: 5
Views: 4100
Reputation: 109547
Try this:
string root = @"C:\Users\me\Desktop\main_folder\";
var files =
Directory.EnumerateDirectories(root).SelectMany(
directory => Directory.EnumerateFiles(directory, "*.zip"));
Upvotes: 8
Reputation: 2166
You can go with LINQ:
string mainFolder = @"C:\Users\me\Desktop\main_folder";
List<string> zipPaths = new List<string>();
string[] subDirectories = Directory.GetDirectories(mainFolder);
subDirectories.ToList().ForEach((path) =>
{
// you can either process your zips here...
// (in this case you don't have to add the paths to the zipPaths list)
zipPaths.AddRange(Directory.GetFiles(path, "*.zip"));
});
// or you can process your zipPaths list here...
All the paths of the ZIP files will be in the zipPaths list.
Upvotes: 1