0

I am tring to get folder files names with specific pattern.

I wrote this:

string[] allfiles = Directory.GetFiles(x, "??-?-??????????.pdf", SearchOption.AllDirectories);

but this works too:

string[] allfiles = Directory.GetFiles(x, "??-?-???.pdf", SearchOption.AllDirectories);

The results are the same:

C:\Users\source\repos\LettersUploader\myFiles\10-9-110000000.pdf
C:\Users\source\repos\LettersUploader\myFiles\11-8-120000000.pdf
C:\Users\source\repos\LettersUploader\myFiles\12-7-130000000.pdf

I want that will be only numbers and in this pattern "11-1-111111111.pdf" so if there will be files that looks like this "11-1-1111.pdf" or "xx-g-atpfgtnjk.pdf" they will not inlcuded.

yanivz
  • 158
  • 1
  • 13

1 Answers1

0

Agree with Jimi; for complex pattern matching it would be better to get all pdfs and then filter those you don't want in c# using regex

var r = new Regex(@"[/\\]\d{2}-\d-\d{9}\.pdf$", RegexOptions.Compiled);

foreach(var f in Directory.GetFiles(x, "*.pdf").Where(r.IsMatch))
  ...

That regex means "forward or backslash, then 2 digits, then hyphen then digit, then hyphen, then 9 digits, then dot pdf then end of string"

Caius Jard
  • 72,509
  • 5
  • 49
  • 80