Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Illegal characters in path" error using wildcards with Directory.GetFiles

Tags:

c#

I have a directory with multiple sub directories that contain .doc files. Example:

C:\Users\user\Documents\testenviroment\Released\test0.doc
C:\Users\user\Documents\testenviroment\Debug\test1.doc
C:\Users\user\Documents1\testenviroment\Debug\test2.doc
C:\Users\user\Documents1\testenviroment\Released\test20.doc

I want to get all the test*.doc files under all Debug folders. I tried:

string[] files = Directory.GetFiles(@"C:\Users\user", "*Debug\\test*.doc",
    SearchOption.AllDirectories);

And it gives me an "Illegal characters in path" error.

If I try:

string[] files = Directory.GetFiles(@"C:\Users\user", "\\Debug\\test*.doc",
    SearchOption.AllDirectories); 

I get a different error: "Could not find a part of the path C:\Users\user\Debug".

like image 619
Me5 Avatar asked Mar 01 '17 12:03

Me5


2 Answers

You are including a folder within the search pattern which isn't expected. According to the docs:

searchPattern Type: System.String The search string to match against the names of files in path. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions.

With this in mind, try something like this:

String[] files = Directory.GetFiles(@"C:\Users\user", "test*.doc", SearchOption.AllDirectories)
                 .Where(file => file.Contains("\\Debug\\"))
                 .ToArray();

This will get ALL the files in your specified directory and return the ones with Debug in the path. With this in mind, try and keep the search directory narrowed down as much as possible.

Note:

My original answer included EnumerateFiles which would work like this (making sure to pass the search option (thanks @CodeCaster)):

String[] files = Directory.EnumerateFiles(@"C:\Users\user", "test*.doc", SearchOption.AllDirectories)
                 .Where(file => file.Contains("\\Debug\\"))
                 .ToArray();

I've just run a test and the second seems to be slower however it might be quicker on a larger folder. Worth keeping in mind.

Edit: Note from @pinkfloydx33

I've actually had that practically take down a system that I had inherited. It was taking so much time trying to return the array and killing the memory footprint as well. Problem was diverted converting over to the enumerable counterparts

So using the second option would be safer for larger directories.

like image 182
webnoob Avatar answered Nov 03 '22 23:11

webnoob


The second parameter, the search pattern, works only for filenames. So you'll need to iterate the directories you want to search, then call Directory.GetFiles(directory, "test*.doc") on each directory.

How to write that code depends on how robust you want it to be and what assumptions you want to make (e.g. "all Debug directories are always two levels into the user's directory" versus "the Debug directory can be at any level into the user's directory").

See How to recursively list all the files in a directory in C#?.

Alternatively, if you want to search all subdirectories and then discard files that don't match your preferences, see Searching for file in directories recursively:

var files = Directory.GetFiles(@"C:\Users\user", "test*.doc", SearchOption.AllDirectories)
               .Where(f => f.IndexOf(@"\debug", StringComparison.OrdinalIgnoreCase) >= 0);

But note that this may be bad for performance, as it'll scan irrelevant directories.

like image 2
CodeCaster Avatar answered Nov 03 '22 22:11

CodeCaster