Since you have tagged C# here are some starter methods.
Get files in a folder, in this case the method includes sub-folders.
public static async Task<List<string>> EnumerateFoldersAsync(string path)
=> await Task.Run(() => Task.FromResult(Directory.EnumerateFiles(path).ToList()));
Call the above using this code, replace the variable folder
with your folder. The variable list
contains a list of files while the variable extensions
list distinct file extensions.
List<string> list = await DirectoryOperations.EnumerateFoldersAsync(folder);
HashSet<string> hashSet = new HashSet<string>();
foreach (var item in list)
{
hashSet.Add(Path.GetExtension(item));
}
var extensions = hashSet.ToList();
extensions.Sort();
To copy files by extension use the following where allowedExtensions in your case is one from the drop down.
public static async Task<List<string>> EnumerateFoldersAsync(string path, string[] allowedExtensions)
=> await Task.Run(() => Task.FromResult(Directory.EnumerateFiles(path).Where(file =>
allowedExtensions.Any(file.ToLower().EndsWith)).ToList()));
Use a modified version of the following code to copy or move files.
In closing, the above code works but will need you to tweak to your specs and by chance if the files may be in a location the user may not have permissions to wrap the code in a try/catch e.g. in this code OnExceptionEvent is an event that the caller (the form) listens for.
public static async Task<(bool success, List<string> list)> EnumerateFoldersAsync1(string path, string[] allowedExtensions)
{
try
{
var list = await Task.Run(() => Task.FromResult(
Directory.EnumerateFiles(path).Where(file =>
allowedExtensions.Any(file.ToLower().EndsWith)).ToList()));
return (true, list);
}
catch (Exception exception)
{
OnExceptionEvent?.Invoke(exception);
return (false, new List<string>());
}
}