Results 1 to 2 of 2

Thread: File locking problem, somtimes

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Oklahoma, USA

    File locking problem, somtimes

    My app unzips a file creating four text files. It then processes the files and deletes the extracted files. My problem is sometimes when I get to the delete it stops with the message that that the file is locked. This does not happen all the time and when I reran the process it wouldn't do it again. It seems lately I have been plagued with these sometimes errors in VB2008.

        Public Function Process_Extracted_Files(ByVal Batch_Id As Integer) As String
            fList = Directory.GetFiles(path1, "*.*")
            For Each f As String In fList
                result = Path.GetFileName(f)
                If IsNumeric(Mid(result, 8, 2)) Then
                    Filename = Path.GetFileName(f)
                            Parse_ACS_V1_COA_File(f, Batch_Id, AccumDate)
                End If
            fList = Directory.GetFiles(path1, "*.*")
            For Each f As String In fList
                If System.IO.File.Exists(f) = True Then
                    File.Delete(f)    <---------------Error happens here             
                End If
        End Function	    
        Public Sub Parse_ACS_V1_COA_File(ByVal InFileName As String, ByVal Batch_Id As Integer, ByVal AccumDate As Date)
            Dim strFileName As String
            Dim ingTotalACS As Long = 0
            Dim Finalcode As String = "000"
            Dim ingHdrTotalACS As Long = 0
            Dim ingHdrTotalNix As Long = 0
            Dim ingHdrTotalCOA As Long = 0
            Dim chrDelCode As Char
            Dim ingTotalCOA As Long = 0
            Dim ingTotalNix As Long = 0
            ' Connect to the database
            Dim dtaHeader As New ImportACS300DataSetTableAdapters.ACS_DataHeaderTableAdapter
            Dim dtaData As New ImportACS300DataSetTableAdapters.ACS_Data_RawTableAdapter
            Dim ds As New ImportACS300DataSet
            dtaHeader.Connection = New OleDb.OleDbConnection(conString)
            dtaData.Connection = New OleDb.OleDbConnection(conString)
            '****         Import from Text               ***
            strFileName = Mid(InFileName, 1 + InStrRev(InFileName, "\"))
            Dim reader = File.OpenText(InFileName)
            Dim strread As String = Nothing
            Dim lines As Integer = 0
            While (reader.Peek() <> -1)
                strread = reader.ReadLine()
                lines = lines + 1
                If Not (Trim(strread) = "") Then 'Ignore blank line
                    'Test for Header Record
                    If "H" = Mid(strread, 1, 1) Then
                        Dim dsNewRow As ImportACS300DataSet.ACS_DataHeaderRow
                        dsNewRow = ds.ACS_DataHeader.NewACS_DataHeaderRow
                        dsNewRow.FileName = strFileName 'Forgin Key to ACS Data
                        dsNewRow.EntryType = "V"
                        dsNewRow.Accumdate = AccumDate
                        dsNewRow.Batch_Id = Batch_Id
                        dsNewRow.CustomerID = CStr(Trim(Mid(strread, 4, 6)))
                        dsNewRow.File_Date = dtACS_Date
                        dsNewRow.TotalACS = CInt(Trim(Mid(strread, 28, 9)))
                        dsNewRow.TotalCOA = CInt(Trim(Mid(strread, 37, 9)))
                        dsNewRow.TotalNIXIE = CInt(Trim(Mid(strread, 46, 9)))
                        dsNewRow.ShipmentNum = CStr(Trim(Mid(strread, 18, 8)))
                    ElseIf "D" = Mid(strread, 1, 1) Then
                        Dim dsNewRow As ImportACS300DataSet.ACS_Data_RawRow
                        dsNewRow = ds.ACS_Data_Raw.NewACS_Data_RawRow
                        dsNewRow.FileName = strFileName  'Forgin Key to ACS Header
                        dsNewRow.RecType = CStr(Trim(Mid(strread, 1, 1)))
                        dsNewRow.File_Ver = CStr(Trim(Mid(strread, 2, 2))) ' New
                        dsNewRow.SeqNum = CStr(Trim(Mid(strread, 4, 8)))
                        dsNewRow.MailerId6 = CStr(Trim(Mid(strread, 12, 6)))
                        dsNewRow.MailPieceId = CStr(Trim(Mid(strread, 21, 16)))
                        dsNewRow.MoveCCYYMMDD = CStr(Trim(Mid(strread, 37, 8)))
                        dsNewRow.MoveType = CStr(Trim(Mid(strread, 45, 1)))
                        dsNewRow.DelivbleCode = CStr(Trim(Mid(strread, 46, 1)))
                        dsNewRow.POSiteId = CStr(Trim(Mid(strread, 47, 3)))
                    ElseIf "" = Mid(strread, 1, 1) Then
                        'MsgBox("EOF Reached")
                        MsgBox("505-This is not a valid record type" & strread)
                    End If
                End If
            End While
            Exit Sub
        End Sub

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    South UK

    Re: File locking problem, somtimes

    Firstly, you should handle the exception by placing that code within a try/catch block. A least the application will then report the problem and continue gracefully.

    You could create a wait loop and try the delete a few times, but you really need to find out which process is locking the file. The "Process Monitor" tool can help with that.

    I don't see where in the code you're calling the Process_Extracted_Files function, is deletion of the files something you could do on exit of the application? (i.e. when connections, streamreaders etc. are all closed).

    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width