none
get files from multiple directories by directory list RRS feed

  • Question

  • using System.Collections.Generic;
    using System.IO;
    
    List<FileInfo> files = new List<FileInfo>();
    DirectoryInfo rootDir = new DirectoryInfo(@"C:\Program Files (x86)\basic\data\");
    var directories = rootDir.GetDirectories("20190314*");
    foreach (var directory in directories)
    {
        files.AddRange(directory.GetFiles());
    }
    
    IEnumerable<string> fileNames = files.Select(f => f.FullName);

    I need to search and file in multiple directories using a list.

    Like regex 201903[10-20] orper month 2019[03,08,10].




    Friday, October 18, 2019 10:24 PM

Answers

  • When posting code please take time to format the code e.g.

    var result = Directory.EnumerateFiles(SelectedFolderPath, "*", SearchOption.AllDirectories)
        .Where(f => System.Text.RegularExpressions.Regex.IsMatch(f, Pattern));
    
    foreach (string files1to3 in result)
    {
        MessageBox.Show(files1to3.ToString());
    }
    Now examine result with a breakpoint using the debugger and see if anything seems incorrect.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Rhadamants Saturday, October 19, 2019 5:30 AM
    Saturday, October 19, 2019 12:02 AM
    Moderator

All replies

  • Using:

    foreach (string files1to3 in Directory.EnumerateFiles(path: SelectedFolderPath, "*",
                                                                              SearchOption.AllDirectories).Where(f => System.Text.RegularExpressions.Regex.IsMatch(f, Pattern)))
                        
    
                        {
                            
                                MessageBox.Show(files1to3.ToString());
                            
                        }

    Give me infinity loop.

    Friday, October 18, 2019 11:07 PM
  • When posting code please take time to format the code e.g.

    var result = Directory.EnumerateFiles(SelectedFolderPath, "*", SearchOption.AllDirectories)
        .Where(f => System.Text.RegularExpressions.Regex.IsMatch(f, Pattern));
    
    foreach (string files1to3 in result)
    {
        MessageBox.Show(files1to3.ToString());
    }
    Now examine result with a breakpoint using the debugger and see if anything seems incorrect.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Rhadamants Saturday, October 19, 2019 5:30 AM
    Saturday, October 19, 2019 12:02 AM
    Moderator
  • Thanks for the tip.
    The problem with the regex loop is because of the directory address.
    How do I use regex to find the directory I need.
    Like:
    @ "C: \ Program Files (x86) \ basic \ data \ 20190314"
    and ignore any other like:
    @ "C: \ Program Files (x86) \ basic \ backup \ 20190314"
    @ "C: \ Program Files (x86) \ basic \ invoice \ 20190314"
    @ "C: \ Program Files (x86) \ basic \ accept \ 20190314"

    It was not a loop. Just found all the "20190314".
    I need to use @ "C: \ Program Files (x86) \ basic \" to find the directories I need. Like invoice and accept.

    Saturday, October 19, 2019 2:26 AM