By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,327 Members | 1,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,327 IT Pros & Developers. It's quick & easy.

Catch if File not Found

P: n/a
Hello, I'm writing a Visual Basic.net (vb.net 03) program and I'm
checking if file(s) exist in a particular directory. If the program
finds such files, I will write the total and names in my log and keep
going. Otherwise, If I find none, I need to write that in my log and
exit the program.

My problem lies when there's no files in that the directory. I can
only get the total (0), but that doesn't get caught as an exception. I
tried different exceptions, but I guess the fact that there are no
files doesn't cause an exception. This is what I have:

Imports System.IO
Imports System.Text
Dim sw As New StreamWriter("c:\mylog.log", True)
Try
' Only get files that have extension *.txt
Dim dirs As String() =
Directory.GetFiles("c:\MyTextFilesDir\", "*.txt")
sw.WriteLine("The number of files with an *.txt extension
is {0}.", dirs.Length)
Dim dir As String
For Each dir In dirs
sw.WriteLine(dir)
Next

Catch ex As FileNotFoundException ' <= I need to modify this!
sw.WriteLine("The process failed: {0}", ex.ToString())
sw.WriteLine("")
sw.WriteLine("Unable to get text file name.")
Exit Sub
End Try
sw.Flush()
sw.Close()

Does anyone knows how to catch when there's no files in a dir and make
that as an exception? Or.. do I need to catch exceptions, or just
check if the total exisinting files = 0?

Thanks

JR.

Mar 27 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Also, is there a way to get only the filename.extension when the
program finds the text files? Right now it's writing the whole path. I
only need "filename.txt"

Mar 27 '06 #2

P: n/a
It will not throw an exception if the directory is empty instead an
empty collection is returned if no files matching the specified pattern
are found. You can look at the length of your dirs array. Also to get
just the name of the file you can use FileInfo and grab the Name
property.

Dim sw As New StreamWriter("c:\mylog.log", True)
Try
' Only get files that have extension *.txt
Dim dirs As String() =
Directory.GetFiles("c:\MyTextFilesDir\", "*.txt")
If dirs.Length <> 0 Then
sw.WriteLine("The number of files with an *.txt
extension is {0}.", dirs.Length)
Else
sw.WriteLine("The number of files with an *.txt
extension is 0.")
End If
Dim dir As String
For Each dir In dirs
Dim file As New FileInfo(dir)
sw.WriteLine(file.Name)
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
sw.Flush()
sw.Close()
sw = Nothing
End Try

Mar 27 '06 #3

P: n/a
It will not throw an exception if the directory is empty instead an
empty collection is returned if no files matching the specified pattern
are found. You can look at the length of your dirs array. Also to get
just the name of the file you can use FileInfo and grab the Name
property.

Dim sw As New StreamWriter("c:\mylog.log", True)
Try
' Only get files that have extension *.txt
Dim dirs As String() =
Directory.GetFiles("c:\MyTextFilesDir\", "*.txt")
If dirs.Length <> 0 Then
sw.WriteLine("The number of files with an *.txt
extension is {0}.", dirs.Length)
Else
sw.WriteLine("The number of files with an *.txt
extension is 0.")
End If
Dim dir As String
For Each dir In dirs
Dim file As New FileInfo(dir)
sw.WriteLine(file.Name)
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
sw.Flush()
sw.Close()
sw = Nothing
End Try

Mar 27 '06 #4

P: n/a
Hello Mr. Doles, Thanks for replying. I had a similar idea about
checking the number of files with an IF statement, so it's quite nice
to know I'm in the right track.

The fileinfo property works like a charm. Thanks for that too.

JR

Mar 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.