Directory.GetFiles

B

brian

I am using the Directory.GetFiles class and am having
problems with the overloaded version. I want to search
files. I can use the following and it works fine:

Directory.GetFiles(Path, k)
Path: c:/Test/
k: 3

It will query all files in the test directory with a file
that is named 3.

I need to add the wildcard behind it to get me all the
files in the test directory that begins with 3. If I
hardcode "3*" it works fine. But 'k' will be changing
all the time so I need a variable of string data type.

I have tried:
(Path, k*)
(Path, "k*")
(Path, k"*")
None of them work. I either get no files or blue
squigglies in other parts of my code.

Any ideas? Can you wildcard search with a paramater?
 
C

CJ Taylor

This is straight from the docs...

So with your k variable do this

(Path, k & "*")

and it will evaluate the string before passing the parameter...

And you will be good.

-cJ


Imports System
Imports System.IO

Public Class Test
Public Shared Sub Main()
Try
' Only get files that begin with the letter "c."
Dim dirs As String() = Directory.GetFiles("c:\", "c*")
Console.WriteLine("The number of files starting with c is {0}.",
dirs.Length)
Dim dir As String
For Each dir In dirs
Console.WriteLine(dir)
Next
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top