Catch if File not Found

I

ILCSP

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.
 
I

ILCSP

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"
 
M

mr_doles

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
 
M

mr_doles

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
 
I

ILCSP

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top