Does file exist
(Thursday, December 23, 2004)
Found the following interesting discussion in the Newsgroups:
Does file exist by:y1799@yahoo.com (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
| | | Reply: by:Sven Groot
| | | Dim path As String = "c:\test" ' or some other directory Dim files() As String = System.IO.Directory.GetFiles(path, "*.txt")
-- Sven Groot
http://unforgiven.bloghorn.com
| | | Reply: by:Stephany
| | | 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.
| | | Reply: by:hirf-spam-me-here@gmx.at (Herfried K. Wagner [MVP])
| | | \\If System.IO.Directory.GetFiles("C:\foo\*.exe").Length > 0 Then MsgBox("File(s) exist!") Else MsgBox("Files do not exist!") End If ///
-- Herfried K. Wagner [MVP]
| | | Reply: by:Stephany
| | | 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
|
|
0 Comments:
Post a Comment