Does file exist

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

Hi,

I am looking for function in .Net library that let me know if exist
any file if I specified template.

Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then
I can get True or at least file name . And if does not exist Fasle or
empty string.

Of course I can use VB6 function Dir, but maybe .Net contains
something inside the class library.

Thanks
 
Mike said:
Hi,

I am looking for function in .Net library that let me know if exist
any file if I specified template.

Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then
I can get True or at least file name . And if does not exist Fasle or
empty string.

Of course I can use VB6 function Dir, but maybe .Net contains
something inside the class library.

Dim path As String = "c:\test" ' or some other directory
Dim files() As String = System.IO.Directory.GetFiles(path, "*.txt")
 
* (e-mail address removed) (Mike) scripsit:
I am looking for function in .Net library that let me know if exist
any file if I specified template.

Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then
I can get True or at least file name . And if does not exist Fasle or
empty string.

\\\
If System.IO.Directory.GetFiles("C:\foo\*.exe").Length > 0 Then
MsgBox("File(s) exist!")
Else
MsgBox("Files do not exist!")
End If
///
 
Just remember that there is a gotcha when using the

Dim files() As String = System.IO.Directory.GetFiles(path, "*.txt")

functionality where the pattern has a 3 character extension. This will
return information for any file in path having an extension that starts with
..txt, (i.e., .txta, .txtb, .txtaa, .txtab, etc.). For extensions of any
other length this is not an issue. Don't ask me why the Redmond gurus made
it that way (I see it as a bug), but it's in the doc's as a design feature.
 
Just remember that there is a gotcha when using the

Dim files() As String = System.IO.Directory.GetFiles(path, "*.txt")

functionality where the pattern has a 3 character extension. This will
return information for any file in path having an extension that starts with
..txt, (i.e., .txta, .txtb, .txtaa, .txtab, etc.). For extensions of any
other length this is not an issue. Don't ask me why the Redmond gurus made
it that way (I see it as a bug), but it's in the doc's as a design feature.

So, for the example to work correclty it needs to be:

Dim _ss() As String = System.IO.Directory.GetFiles("C:\foo", "*.exe")

Dim _f As Boolean = False

For Each _s As String In _ss
If System.IO.Path.GetExtension(_s) = ".exe") Then
_f = True
Exit For
End If
Next

If _f Then
MsgBox("File(s) exist!")
Else
MsgBox("Files do not exist!")
End If
 
Back
Top