Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a folder inside which there are other sub-folders in the structure
2017-12456-12463-1023\2017\12456\12463\1023\abc.txt
2016-32456-15463-2033\2016\32456\15463\2033\xyz.txt
... so on ...
Now, there are other sub-folders inside each of the folders
2017-12456-12463-1023
2016-32456-15463-2033
which contains txt files
How do I get only the txt files from folders 1023, 2033 in the above mentioned folder structures.
In VB I used something like

What I have tried:

VB
Dim txtFiles = Directory.EnumerateFiles(targetDirectory, "*.txt", SearchOption.AllDirectories).Where(Function(f) f Like "*\#*\#*\#*\#*.txt").ToArray


But how do I do it in c#?
Posted
Updated 24-Nov-17 14:50pm

VB.NET and C# use all the same classes so it should be exactly the same. You can check it out from Directory.EnumerateFiles Method (System.IO)[^].
 
Share this answer
 
Comments
phil.o 24-Nov-17 12:11pm    
Nope :) There is no equivalent in C# to the Like VB.NET operator which operates on strings, comparable to the SQL LIKE operator.
Richard MacCutchan 24-Nov-17 12:23pm    
If you go to the link I gave you there is some sample C# code with LINQ to do what you want.
phil.o 24-Nov-17 12:54pm    
I'm not the OP :)
And the issue here is not technically on the EnumerateFiles method, it is on the lambda passed as parameter to the Where function: VB.NET has a LIKE operator which allows to search for a pattern using wildcards; C# doesn't.
I guess a regular expression here could do it.
The problem here is the OP only wants the files at a given depth, not the files in the parent directories, and not the files in the subdrectories of the target directory. The search pattern parameter of the EnumerateFiles method does not allow that per se.
Richard MacCutchan 24-Nov-17 13:18pm    
Looking at the sample in the documentation it appeared to provide the same functionality. And I assumed the LIKE operator was a .NET feature.

Thanks for the correction.
Member 12692000 24-Nov-17 20:31pm    
How can I use regular expression to this? Can you show some code? Also the
parent folders and its sub-folder have a fixed pattern as shown in the question.
Sorry for the late solution but I've been quite busy today with my own stuff.
You can try:
C#
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

// ...

// First retrieve all files in the hierarchy
IEnumerable<string> allFiles = Directory.EnumerateFiles(targetDirectory, "*.txt", SearchOptions.AllDirectories);

// Then isolate only those which are in the directory you are interested in
Regex r = new Regex(@".+\\[\d]+\\[\d]+\\[\d]+\\[\d]+\\[^\\]+\.txt", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string[] files = allFiles.Where(f => r.IsMatch(f)).ToArray();

The regex is a quick-built, but I used Expresso[^] to test it and it seems correct.

Hope this helps. Kindly.

[Edit]: Corrected a mistake where I used the Match method instead of the proper IsMatch method.

[Edit 2]:
C#
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

class YourClass
{
   private static readonly Regex regex = new Regex(@".+\\[\d]+\\[\d]+\\[\d]+\\[\d]+\\[^\\]+\.txt", RegexOptions.Compiled | RegexOptions.IgnoreCase);

   // ...

   // In your method:
   string[] files = Directory.EnumerateFiles(targetDirectory, "*.txt", SearchOptions.AllDirectories).Where(f => regex.IsMatch(f)).ToArray();
}
 
Share this answer
 
v3
Comments
Member 12692000 25-Nov-17 10:43am    
Thanks @phil.o.
BTW, could this be done in a single line of code?
phil.o 25-Nov-17 11:02am    
Yes, sure. If you are going to use the Regex more than once, I advise you to make it a static class variable, as a Regex instantiation is a rather costly process.
I'll edit my solution in a few minutes and show you how.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900