Link to home
Start Free TrialLog in
Avatar of bschave2
bschave2

asked on

how do i fix cannot delete file because it is being used by another process error

I have an .EXE application function function that reads files in a directory within a directory. It then pushed function to put the files in a db. then after the loadimages function does that, it loops through the directory it just loaded into the db and deletes them from the directory. Now sometimes when this exe is run there is no error, but other times there is an error. Does any body have an idea of why this is happening sometimes, but not everytime? Also, does anyone know how to fix this issue?

I am attaching the function that is receiving the error and the full error received.

FULL ERROR RECEIVED:

************ Job: Started  At Mon 04/05/2010 18:30:14.89 *********
********* Step 1: Started  At Mon 04/05/2010 18:30:15.00 *********

C:\>"\\msilsh0010V049\TAGJobs\1IAM_MailGetWork_C\MailGetWork.exe" /A ENV=Prod /A LogPath="\\msilsh0010V049\TAGJobs\1IAM_MailGetWork_C\Logs\" /A TRN=MMTG1IAMP001,11210  /A MAILSRC=\\msilsh0010V049\TagFileNet\MailWF

Unhandled Exception: System.IO.IOException: The process cannot access the file '\\msilsh0010V049\TagFileNet\MailWF\ScannedImages\LMO2010-04-05155003\INDEX.TMP' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.Delete(String path)
   at MailGetWork.GetWork.deleteAllFiles(String dir)
   at MailGetWork.GetWork.LoadScannedImages(String Env, String MailSrc)
   at MailGetWork.GetWork.Main()

C:\>ECHO OFF
********* Step 1: Finished At Mon 04/05/2010 18:31:40.42 *********

FUNCTION WHERE THE ERROR IS COMING FROM

Private Function LoadScannedImages(ByVal Env As String, ByVal MailSrc As String) As Boolean
        Dim DepartmentFolder() As String
        Dim FileFolder() As String
        Dim Path As String
        Dim i As Integer
        Dim ErrorInDepartment, ErrorInScannedImages, MailDoc As Boolean

        ErrorInScannedImages = False

        Path = MailSrc & "ScannedImages"
        DepartmentFolder = Directory.GetDirectories(Path, "*")

        For i = 0 To DepartmentFolder.Length - 1
            ErrorInDepartment = False
            MailDoc = True                                              ' Some images are handled outside MailWF
            If UCase(Mid(DepartmentFolder(i), InStrRev(DepartmentFolder(i), "\") + 1, 3)) = "SBP" Then MailDoc = False
            If MailDoc Then
                ErrorInDepartment = LoadDepartmentMail(DepartmentFolder(i))

                If File.Exists(DepartmentFolder(i) & "\INDEX.DAT") Then
                    File.Delete(DepartmentFolder(i) & "\INDEX.DAT")
                End If
                If ErrorInDepartment Then
                    ErrorInScannedImages = True
                    WriteToLogFile(LogFileName, "Error in folder " & DepartmentFolder(i) & ".  Folder not processed.")
                Else
                    deleteAllFiles(DepartmentFolder(i))
                    Directory.Delete(DepartmentFolder(i))
                End If
            Else
                WriteToLogFile(LogFileName, "Bypassing " & DepartmentFolder(i))
            End If
        Next

        FileFolder = Directory.GetFiles(Path, "*")                      ' Scanner places a file in folder with
        For i = 0 To FileFolder.Length - 1                              ' stats of scan.  Don't need it so delete.
            File.Delete(FileFolder(i))
        Next

        Return ErrorInScannedImages

    End Function
ASKER CERTIFIED SOLUTION
Avatar of Tarakas4
Tarakas4
Flag of Malawi image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Faced a similar problem some times back. I moved the document to a temporary folder and deleted from the original folder. After processing I deleted from the temporary folder. The reason why it fails to delete sometimes is that the system could still be referring to it- perhaps you can disconnect from the database completely.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bschave2
bschave2

ASKER

it's a false error. The directory cannot be delted if there are files in the directory. Specifically .tmp files. So, I just through a try and catch in there because if the directory doesn't delete the other automated program knows to just add more files into the directory rather than create a whole new directory. Thanks for the help though.