Get only .pdf and .docx from directory

Hello,

Is there a way of getting only two specific file formats from a directory? I am currently attempting to read .docx and .pdf from a folder with Directory.GetFiles but there is a validation error:

I can specify one format, but not the other. How would I get both formats into one array?

Thanks in advance.

Hi,

Can you try the following expression?

System.IO.Directory.GetFiles(path).Where(Function(f) System.Text.RegularExpressions.Regex.IsMatch(System.IO.Path.GetExtension(f).ToLower,"docx|jpg")).toArray()

Regards,

2 Likes

Hey @Yoichi,

This works great. I modified the list to pick up PDFs instead of JPEGs.

1 Like

Hi,

There is supplemental information.

It might be higher maintainability to have each extension in an array in advance as the following.

exts = {".docx",".pdf"}
files = System.IO.Directory.GetFiles(path).Where(Function(f) exts.Contains(System.IO.Path.GetExtension(f).ToLower)).toArray()

Regards,

1 Like

Hey @Yoichi,

This is a good idea, but we have agreed with the client that we are using only .docx and .pdf formats, so I shouldn’t need to set an array in the beginning. Thank you for this though.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.