How do we find and list catgorywise file from specific folder

BeUnique 2,112 Reputation points
2021-12-16T09:34:03.417+00:00

I am developing small software and bit little bit confused whether we can go for VBA or .NET EXE or web app.

I have source drive which contains many type of file's like (excel,word,pdf,drawing,etc).

First read all the files from the source files and then list only extension of the all files (Distinct) like (.xls, .word, .txt, etc) and then bound those types in dropdown.

whenever user selects the type of the files and destination folder, then all related files to be moved from source to destination folder.

for example, if user select .txt file from the dropdown, all the .txt file from selected source drive must be move to destination folder.

i am little bit confused in which languages i have to use for this requirement (like either i have to use VBA or .EXE or web app).

pls. suggest. below is my design screenshot.

158203-image.png

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
783 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,907 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,421 Reputation points
    2021-12-16T11:42:42.637+00:00

    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>());
        }
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.