Group,

In a post in VB 4/5/6, I question how to open a file with a wildcard as the file extension (https://www.daniweb.com/software-development/visual-basic-4-5-6/threads/488809/accessing-a-file-with-a-ever-changing-name. That worked fine for VBA. However I'm trying to do the same thing using VB.net. I'm having trouble finding the correct syntax. I hope you can help.

Specifically, I have to find a file that has a varying file extension. The file name will be the creation date. The file extension is the time the file was created. So the file name looks like this: "20141216.042615". So I need to write my code so that it will go and find "20141216.(asterisk)" or whatever the appropriate wildcard character is. I've tried

Dim RestranName = "C:\Restran Conversion\IPSDATA\PM00213A\20141210.%"

However the error in the Immediate box says "A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll". So is there an appropriate way to connect with the file?

In advance, thanks for your help.

Don

I failed to mention that the file, "20141216.042615", was produced via a UNIX based program (I believe, but not completely sure).

One way is to use System.IO.Directory.GetFiles

Dim wantedfiles = System.IO.Directory.GetFiles("C:\Restran Conversion\IPSDATA\PM00213A", "20141210.*")(0)

GetFiles actually returns an array but if you know there will only be one, just use index 0

 Dim dirs As String() = Directory.GetFiles("C:\Restran Conversion\IPSDATA\PM00213A\", "*.*")
 Dim dir As String
        For Each dir In dirs
            Debug.Print(dir)
        Next

Minimalist, what is the comma for (Dim dirs As String() = Directory.GetFiles("C:\Restran Conversion\IPSDATA\PM00213A\", "*.*"))? I would have assumed you would use and ampersand there. I see another teaching moment!

Thanks again.

I just copied the path from above assuming thats what it is. Otherwise:
Dim dirs As String() = Directory.GetFiles("C:\Restran Conversion\IPSDATA\PM00213A\" & ".")

Minimalist/tinstaafl, I've discovered this code causes an error message on a line further down in my code. It says:

Using SR As New System.IO.StreamReader(RestranName)

The variable "SR" has the error. The message says,

"Error 1 Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(path As String)': Value of type '1-dimensional array of String' cannot be converted to 'String'.
'Public Sub New(stream As System.IO.Stream)': Value of type '1-dimensional array of String' cannot be converted to 'System.IO.Stream'.

Is there a fix for this?

Try this:Using SR As New System.IO.StreamReader(RestranName(0)) Getfiles returns an array of file names. Since you seem to only expect 1 you have to specify the index. Either as I've show you here or as I showed you in my first answer.

tinsaafl/Minimalist, I've made the changes you've suggested and have run the following:

Dim RestranName As String() = System.IO.Directory.GetFiles("C:\Restran Conversion\IPSDATA\PM00213A\20141210.", "*")

It's not finding the file. If I type in the actual path (C:\Restran Conversion\IPSDATA\PM00213A\20141210.142601), it finds it with no issues. This wildcard is so important. Any thoughts on what needs to be done?

The parameters for GetFiles is the path to the directory you want to search and the search criteria. You're passing part of the file name as the directory you want to search. If you follow my original answer you should come up with something like this:

Dim RestranName As String() = System.IO.Directory.GetFiles("C:\Restran Conversion\IPSDATA\PM00213A","20141210.*")

This will find every file name that starts with "20141210.".

Bingo! tinsaafl, you're the man!

Thank you, thank you, thank you!

Don

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.