Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make GetFiles() exclude files with extensions that start with the search extension?

I am using the following line to return specific files...

FileInfo file in nodeDirInfo.GetFiles("*.sbs", option)

But there are other files in the directory with the extension .sbsar, and it is getting them, too. How can I differentiate between .sbs and .sbsar in the search pattern?

like image 233
topofsteel Avatar asked Nov 27 '13 11:11

topofsteel


People also ask

What is the use of the public function GetFiles as FileInfo()?

Public Function GetFiles As FileInfo()Returns a file list from the current directory. For complete list of properties and methods please visit Microsoft's documentation.

What does directory GetFiles return?

GetFiles(String) Returns the names of files (including their paths) in the specified directory.


1 Answers

The issue you're experiencing is a limitation of the search pattern, in the Win32 API.

A searchPattern with a file extension (for example *.txt) of exactly three characters returns files having an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern.

My solution is to manually filter the results, using Linq:

nodeDirInfo.GetFiles("*.sbs", option).Where(s => s.EndsWith(".sbs"),
    StringComparison.InvariantCultureIgnoreCase));
like image 110
Daniel Peñalba Avatar answered Sep 18 '22 05:09

Daniel Peñalba