tags:

views:

23

answers:

3

I am using following code to search a specific file with .doc extension. How can I get this info into a variable to be used later. Thanks

Dim di As New DirectoryInfo("d:\")
Dim files() As FileInfo = di.GetFiles("*.doc", SearchOption.AllDirectories)
+1  A: 

You already do have the info, in your files() array.

You can then use files() array to get a count of matches files.Length, or iterate through the matching files foreach file as FileInfo in files {}.

PaulG
A: 

Not sure what the issue is, but the files variable in your code sample already contains this information.

This line:

Dim files() As FileInfo = di.GetFiles("*.doc", SearchOption.AllDirectories)

Gets the file information and assigns it to the files() array. This can now be used as it contains the information returned by the GetFiles method.

Oded
That is right. But when I try to get the path bymsgbox(files(1))It throws an error "Prompt Can not be converted into String"I need to display the path of each file obtained in a message boxThanks
@user415037 - Each item in the `files` array is a [`FileInfo`](http://msdn.microsoft.com/en-us/library/system.io.fileinfo_members.aspx) object, not a file path. You can do `msgbox(files(1).FullName))` to get the full path.
Oded
This works well ! Thank youHow Can I know how many elements are there in Files(), so I could run a loop.Thnaks
@user415037 - Use `files.GetUpperBound(0)`. You need to learn about arrays and how to use them. Here is a useful link: http://www.vbdotnetheaven.com/UploadFile/ssivkumar/ExploringArrays04212005060650AM/ExploringArrays.aspx
Oded
A: 

From the code you've written, you've already got all the files .doc files in the files() array.

What exactly do you want to do?

Alex Essilfie
That is right. But when I try to get the path bymsgbox(files(1))It throws an error "Prompt Can not be converted into String"I need to display the path of each file obtained in a message boxThanks