Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scan a directory with wildcard with a specific subdirectory

I was wondering what would be a good way to scan a directory that has characters you are not sure of.

For example, I want to scan

C:\Program\Version2.*\Files

Meaning

  • The folder is located in C:\Program
  • Version2.* could be anything like Version2.33, Version2.1, etc.
  • That folder has a folder named Files in it

I know that I could do something like foreach (directory) if contains("Version2."), but I was wondering if there was a better way of doing so.

like image 219
00101010 10101010 Avatar asked Dec 11 '12 07:12

00101010 10101010


People also ask

Which are the wildcard characters for finding a file folder?

The Directory Path field supports standard common wildcard asterisk (*) or the question mark (?) for matching directory names.

What is the use of * wild card character when we search a file?

To locate a specific item when you can't remember exactly how it is spelled, try using a wildcard character in a query. Wildcards are special characters that can stand in for unknown characters in a text value and are handy for locating multiple items with similar, but not identical data.

How do I find subfolders in Linux?

Finding Files Recursively in Linux The find command does not need flags to search the files recursively in the current directory. You only need to define the main directory and the file name using the –name option. This command will search the file within the main directory and all subdirectories.

What is a wildcard path?

Rather than entering each file by name, using wildcards in the Source path allows you to collect all files of a certain type within one or more directories, or many files from many directories.


1 Answers

Directory.EnumerateDirectories accepts search pattern. So enumerate parent that has wildcard and than enumerate the rest:

  var directories = 
    Directory.EnumerateDirectories(@"C:\Program\", "Version2.*")
     .SelectMany(parent => Directory.EnumerateDirectories(parent,"Files"))

Note: if path can contain wildcards on any level - simply normalize path and split by "\", than collect folders level by level.

like image 176
Alexei Levenkov Avatar answered Sep 28 '22 19:09

Alexei Levenkov