Error Handling question

C0d3M0nk3y

Member
Joined
May 6, 2011
Messages
10
Programming Experience
Beginner
I saw several posts regarding error handling but was not sure where to post my specific question so went ahead and started a new thread.

I am moving along well with my application thanks to help from others previously on this forum however I am up again a wall again and need a little guidance.

I have an application that will recursive find files based on criteria specified in a config file (search by extension, path, file size, etc.) however the application also gives the ability to delete files based on these criteria as well. I have handles the permissions to the files ie if they are read-only etc, however if the settings are set to delete the files I want to catch any errors that might occur if the file is not able to be deleted. I don't want to do a try because i want the loop to continue if an error is received when trying to delete the file as it might be successfull deleting other files. I then want to log each file that is not successfully deleted to a temp file.

any help would be appreciated!

Thanks,
-VB Oober Noob!

Sub fileSizeGreaterWAge(ByVal activeStat As String, ByVal rootDirectory As String, _
ByVal fileExtension As String, ByVal maxAge As Int32, ByVal fileSize As Int32, _
ByVal fileOperand As String, ByVal recursive As String)
Dim sfile As FileInfo
Dim tempSuccessFileName As String = IO.Path.GetTempFileName()
Dim tempFailureFileName As String = IO.Path.GetTempFileName()
Dim stemp As New IO.FileStream(tempSuccessFileName, IO.FileMode.Create)
Dim ftemp As New IO.FileStream(tempFailureFileName, IO.FileMode.Create)
Dim swriteFile As New StreamWriter(stemp)
Dim fwriteFile As New StreamWriter(ftemp)
Dim di As DirectoryInfo = New DirectoryInfo(rootDirectory)
Dim rfiles() As FileInfo = _
di.GetFiles(fileExtension,
SearchOption.AllDirectories)
Dim files() As FileInfo = _
di.GetFiles(fileExtension,
SearchOption.TopDirectoryOnly)
Dim failureSubject As String
Dim failureBody As String

On Error Resume Next
If recursive = "True" Then
For Each sfile In rfiles
If fileSize = ">" And maxAge <> "" Then
If (Now - sfile.CreationTime).Days > maxAge And sfile.Length > fileSize Then
Dim filePermission As FileIOPermission = New _
FileIOPermission(FileIOPermissionAccess.AllAccess, sfile.FullName)
filePermission.Assert()
If File.GetAttributes(sfile.FullName) = FileAttributes.ReadOnly Then
File.SetAttributes(sfile.FullName, FileAttributes.Normal)
File.Delete(sfile.FullName)
swriteFile.WriteLine(
"File Name: " & cReturn & sfile.FullName & "File Size: " & _
sfile.Length &
" byte(s)" & cReturn & "File Created: " & _
sfile.CreationTime & cReturn)
End If
End If
Else
swriteFile.WriteLine("There are no files to be deleted at this time")
End If
Next sfile
End If
If recursive = "False" Then
For Each sfile In files
If fileSize = ">" And maxAge <> "" Then
If (Now - sfile.CreationTime).Days > maxAge And sfile.Length > fileSize Then
Dim filePermission As FileIOPermission = New _
FileIOPermission(FileIOPermissionAccess.AllAccess, sfile.FullName)
filePermission.Assert()
If File.GetAttributes(sfile.FullName) = FileAttributes.ReadOnly Then
File.SetAttributes(sfile.FullName, FileAttributes.Normal)
File.Delete(sfile.FullName)
swriteFile.WriteLine(
"File Name: " & cReturn & sfile.FullName & "File Size: " & _
sfile.Length &
" byte(s)" & cReturn & "File Created: " & _
sfile.CreationTime & cReturn)
End If
End If
Else
swriteFile.WriteLine("There are no files to be deleted at this time")
End If
Next sfile
End If
If recursive <> "False" And recursive <> "True" Then
failureSubject = "Job did not complete Successfully"
failureBody = """recursive"" parameter must be set to a value of ""True"" or ""False"" in the config file." _
& cReturn & cReturn &
"Please check the config file and try again."
Call SendMailFailureMessage("", failureSubject, failureBody)
End If
Console.WriteLine(tempSuccessFileName)
swriteFile.Close()
End Sub
 
Sub fileSizeGreaterWAge(ByVal activeStat AsString, ByVal rootDirectory AsString, _
ByVal fileExtension AsString, ByVal maxAge AsInt32, ByVal fileSize AsInt32, _
ByVal fileOperand AsString, ByVal recursive AsString)
Dim sfile AsFileInfo
Dim tempSuccessFileName AsString = IO.Path.GetTempFileName()
Dim tempFailureFileName AsString = IO.Path.GetTempFileName()
Dim stemp AsNew IO.FileStream(tempSuccessFileName, IO.FileMode.Create)
Dim ftemp AsNew IO.FileStream(tempFailureFileName, IO.FileMode.Create)
Dim swriteFile AsNewStreamWriter(stemp)
Dim fwriteFile AsNewStreamWriter(ftemp)
Dim di AsDirectoryInfo = NewDirectoryInfo(rootDirectory)
Dim rfiles() AsFileInfo = _
di.GetFiles(fileExtension, SearchOption.AllDirectories)
Dim files() AsFileInfo = _
di.GetFiles(fileExtension, SearchOption.TopDirectoryOnly)
Dim failureSubject AsString
Dim failureBody AsString

OnErrorResumeNext
If recursive = "True"Then
ForEach sfile In rfiles
If fileSize = ">"And maxAge <> ""Then
If (Now - sfile.CreationTime).Days > maxAge And sfile.Length > fileSize Then
Dim filePermission AsFileIOPermission = New _
FileIOPermission(FileIOPermissionAccess.AllAccess, sfile.FullName)
filePermission.Assert()
IfFile.GetAttributes(sfile.FullName) = FileAttributes.ReadOnly Then
File.SetAttributes(sfile.FullName, FileAttributes.Normal)
File.Delete(sfile.FullName)
swriteFile.WriteLine("File Name: " & cReturn & sfile.FullName & "File Size: " & _
sfile.Length & " byte(s)" & cReturn & "File Created: " & _
sfile.CreationTime & cReturn)
EndIf
EndIf
Else
swriteFile.WriteLine("There are no files to be deleted at this time")
EndIf
Next sfile
EndIf
If recursive = "False"Then
ForEach sfile In files
If fileSize = ">"And maxAge <> ""Then
If (Now - sfile.CreationTime).Days > maxAge And sfile.Length > fileSize Then
Dim filePermission AsFileIOPermission = New _
FileIOPermission(FileIOPermissionAccess.AllAccess, sfile.FullName)
filePermission.Assert()
IfFile.GetAttributes(sfile.FullName) = FileAttributes.ReadOnly Then
File.SetAttributes(sfile.FullName, FileAttributes.Normal)
File.Delete(sfile.FullName)
swriteFile.WriteLine("File Name: " & cReturn & sfile.FullName & "File Size: " & _
sfile.Length & " byte(s)" & cReturn & "File Created: " & _
sfile.CreationTime & cReturn)
EndIf
EndIf
Else
swriteFile.WriteLine("There are no files to be deleted at this time")
EndIf
Next sfile
EndIf
If recursive <> "False"And recursive <> "True"Then
failureSubject = "Job did not complete Successfully"
failureBody = """recursive"" parameter must be set to a value of ""True"" or ""False"" in the config file." _
& cReturn & cReturn & "Please check the config file and try again."
Call SendMailFailureMessage("", failureSubject, failureBody)
EndIf
Console.WriteLine(tempSuccessFileName)
swriteFile.Close()
EndSub


Try using the new [ xcode = vb ] [ / xcode ] tags around your code or [ code ] [ /code ] tags when posting code, especially larger blocks.

Edit: thought it would come out prettier but still a good idea to use the code tags.

And one thing I noticed is on error resume next is an obsolete command that you should replace with:

VB.NET:
Try
'code
Catch Ex As Exception
'leave blank
End Try

You can put a try block around the lines where you anticipate an error and just leave the catch block blank if you wish to "swallow" the error. Obviously one try block around all that code will not accomplish what you want but multiple try blocks around the lines with potential errors should get the job done.
 
Last edited:
You can also use Try to get the error message, ex:

Try
  'code here
Catch Ex As Execption
  MsgBox(Ex.Message)
  'You can use the 'Ex.Message' to get the error
End Try
 
Back
Top