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