Reputation: 3
I have the following LINQ query to filter files:
Dim arrayFiles As FileInfo()
Dim strFolder As String = "C:\Temp"
Dim strSearch As String = "[AFile].*"
Dim directoryInfo As DirectoryInfo = New IO.DirectoryInfo(Path:=strFolder)
arrayFiles = directoryInfo.GetFiles(searchPattern:=strSearch,
searchOption:=SearchOption.TopDirectoryOnly).
Where(Function(s) s.Name.ToUpper Like strSearch.ToUpper).
ToArray()
In this example the specified folder contains 1 files: "[AFile].PDF". The above example returns 0 files (array is empty).
If I change the search pattern to "AFile.PDF" or "*.*" then I do get the correct array with 1 file.
Why does the search pattern "AFile.*" doesn't work in this example?
Upvotes: 0
Views: 188
Reputation: 16898
You are providing a search pattern to GetFiles so there is no need to use additional Where
:
arrayFiles = (New IO.DirectoryInfo(Path:=strFolder)).GetFiles(searchPattern:=strSearch,
searchOption:=SearchOption.TopDirectoryOnly).ToArray
Your code is not working because []
brackets have a special meaning in VB.NET LIKE operator:
[ charlist ] - Any single character in charlist
Hence you need to escape this special characters by enclosing them in brackets:
Dim strSearch As String = "[[]AFile[]].*"
Note: In fact you can escape only [
because ]
has special meaning only when it is paired with [
.
Upvotes: 1