Reputation: 6130
I want to show all PDF filenames that has "33" in any position.
sample pdf list on PDFFiles Folder
1111.pdf
3311.pdf
2222.pdf
2331.pdf
1234.pdf
1233.pdf
I need to get result like this,that is something like wildcard %33% on sql
3311.pdf
2331.pdf
1233.pdf
I tried this one
Me.ListBox1.Items.AddRange(Directory.GetFiles("C:\PDFFiles", "*33*" & ".PDF", SearchOption.AllDirectories))
but it still displays all the pdf files.
1111.pdf
3311.pdf
2222.pdf
2331.pdf
1234.pdf
1233.pdf
and this
Me.ListBox1.Items.AddRange(Directory.GetFiles("C:\PDFFiles", "*33" & ".PDF", SearchOption.AllDirectories))
but it only get
1233.pdf //this get all filename that ends with 33
Thanks in Regards
Upvotes: 1
Views: 10965
Reputation: 6130
We have found out that
Filter *___*
works if the string has lenght greater than or equal to 4.
So if i want to get all records that has test name value:
test1234.pdf
abcdefg.pdf
123test45.pdf
12345678.pdf
My filter should be: "*test" & ".PDF"
it will give the desired result
test1234.pdf
123test45.pdf
FYI
Upvotes: 0
Reputation: 942418
The pattern matching algorithm for wildcards are rather strange if you are used to regular expressions. There's a lot of history behind it, going back through Windows 3, MS-DOS, CP/M (an operating system for 8-bit machines) and RSX (an operating system on 16-bit DEC machines). With heavy borrowing between them, including the wildcard behavior. Some accidental commonality btw, David Cutler was the principal architect behind the first and the last one.
Anyhoo, *33*
isn't going to work. You'll need to apply your own filter. Search for *.*
or *.pdf
and use Path.GetFileNameWithoutExtension() and String.Contains() to find the matches.
Upvotes: 2
Reputation: 50712
I have noticed this behavior too when using more than one *
.
I solved it by getting all file names and then filtering the correct names by using LINQ:
Dim allFileNames as String() = _
Directory.GetFiles("C:\PDFFiles", "*.PDF", SearchOption.AllDirectories)
Dim filtered As IEnumerable(Of String) = _
.Where(Function(fileName) Path.GetFileNameWithoutExtension(fileName).Contains("33"))
Upvotes: 2
Reputation: 223422
You are missing *
at the end of "*33"
, put one more star at the end of like "*33*"
.
Your current expression : "*33" & ".PDF" means, All file names which ends with 33.PDF
that is why you are getting 1233.pdf
and not 2331.pdf
EDIT: Directory.GetFileName()
Search pattern similar to
"*1*.txt"
may return unexpected file names. For example, using a search pattern of "1.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".
Upvotes: 0