473,327 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Does file exist

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
Nov 20 '05 #1
4 4026
Mike wrote:
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")

--
Sven Groot

http://unforgiven.bloghorn.com
Nov 20 '05 #2
* y1***@yahoo.com (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").Lengt h > 0 Then
MsgBox("File(s) exist!")
Else
MsgBox("Files do not exist!")
End If
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
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.

"Sven Groot" <sv*******@gmx.net> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Mike wrote:
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")

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #4
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2h************@uni-berlin.de...
* y1***@yahoo.com (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").Lengt h > 0 Then
MsgBox("File(s) exist!")
Else
MsgBox("Files do not exist!")
End If
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Daniel Polansky | last post by:
Is there a way to open file for writing so that the opening fails if the file already exist? This needs to be atomic operation so that it is possible to use the file as a semaphore. As a result,...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
0
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). ...
3
by: Olivogt | last post by:
Hello, I was just puting an application on the web server but it did not work as usual... - I do develop on my notebook and move released applications to the Web server - both have Sql Server...
16
by: lawrence k | last post by:
I've a file upload script on my site. I just now used it to upload a small text document (10k). Everything worked fine. Then I tried to upload a 5.3 meg Quicktime video. Didn't work. I've...
3
by: Mike | last post by:
Thanks for the reply, I have been trying that, but I keep getting the same results. The result I get is that the file exits, when it really doesn't. All my msgbox display twice and I'm not sure...
1
by: Atia Amin | last post by:
Hi, I am a new member. Hello to every one. I am new in ASP.NET area. I wrote an ASP.NET web application which was running ok with my old laptop. Currently I have given a new laptop. Now I copied...
20
by: Bob Sanderson | last post by:
This is my code: if (file_exists($Fname)) { echo "<td>$Fname exists</td>"; } else { echo "<td>$Fname does not exist</td>"; } $Fname is the full path to the file I'm trying to verify. When I...
2
ADezii
by: ADezii | last post by:
Many times when writing VBA code, we find the need to work with Files in one form or another. Whatever the process may be, we need to be sure that a File is present in a specified location. One...
0
by: KingKong07 | last post by:
Hi! I have a problem with publishing my solution. I use “Use fixed naming and single page assemblies” to publish. So I just can update one and one file, this works fine, but today I had to update...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.