[RESOLVED] how to trap an error reading CDrom
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] how to trap an error reading CDrom

  1. #1
    Join Date
    Oct 2009
    Posts
    12

    [RESOLVED] how to trap an error reading CDrom

    I'm writing a program that list the contents of a CDrom and also the contents of the ZIP files.
    When there is a bad Zip file on the CD, the program keeps traying to reed the file and after +- 50 times it gives the error and ends the function.
    This function is called from an other function that scans al the files on the disc.
    Is it possible to give an error but that the program continues with the next file ?
    Code:
    'FUNCTION THAT SCANS THE FOLDER
    Private Function Write_Files(ByVal START_FOLDER) 
    Dim DE_FILE As Scripting.File 
    Dim SUBFOLDER As Scripting.Folder 
       Label_List_FolderBezig.Caption = START_FOLDER 
       Label_List_FolderBezig.Refresh 
       For Each DE_FILE In START_FOLDER.Files 
          On Error GoTo Volgende 
          Call Write_In_Textbox(DE_FILE.PATH) 
    Volgende: 
       Next 
       For Each SUBFOLDER In START_FOLDER.SubFolders 
          Call Write_Files(SUBFOLDER) 
       Next 
    End Function 
    
    
    'FUNCTION WRITING THE CONTENTS OF THE FILE
    Private Function Write_In_Textbox(ByVal PATH_FILENAME As String) 
    Dim FileName As String 
    Dim LENGTE As String 
    '§ == for listing ZIP files == 
    Dim ReadHead As typZipLocalFileHead 
    Dim FNum As Integer 
    Dim FileString As String 
    Dim SeekSize As Long 
    Const ZipLocalFileHeadSig As Long = &H4034B50 
    '§ ================ 
       FileName = Right(PATH_FILENAME, Len(PATH_FILENAME) - InStrRev(PATH_FILENAME, "\")) 
       If Check_add_path Then 
    '§ Path en filename 
          If Check_AddDriveName Then 
             List_Lijst.AddItem PATH_FILENAME 
          Else 
             List_Lijst.AddItem Mid(PATH_FILENAME, InStr(PATH_FILENAME, "\") + 1) 
          End If 
       Else 
    '§ only filename 
          List_Lijst.AddItem FileName 
       End If 
    '§ is it a ZIP file: list the contents if asked 
       If (Right(FileName, 4) = ".zip" Or Right(FileName, 4) = ".ZIP") And _ 
          Check_ExploreZIP Then 
    '§ Make sure file exists 
          On Error Resume Next 
          LENGTE = FileLen(PATH_FILENAME) 
          If LENGTE <> "" Then 
    '&#167; Get a free file handle and open the file 
             FNum = FreeFile() 
             On Error Resume Next 
             Open PATH_FILENAME For Binary Access Read Lock Write As #FNum 
    '&#167; Read a chunk signature 
                Do 
                   On Error GoTo Error_in_File 
                   Get #FNum, , ReadHead.zlfhSignature 
    '&#167; Check for a local file header signature 
                   If (ReadHead.zlfhSignature = ZipLocalFileHeadSig) Then 
    '&#167; Ok, read the full structure 
                       Seek #FNum, Seek(FNum) - 4 
                       Get #FNum, , ReadHead 
                       With ReadHead 
    '&#167; Get the file name 
                           If (.zlfhFileNameLength) Then 
                               FileString = Space(.zlfhFileNameLength) 
                               Get #FNum, , FileString 
                               If Mid(FileString, Len(FileString)) <> "/" And _ 
                                  FileString <> "" Then 
                                  If Check_add_path Then 
    '&#167; Path en filename 
                                     If Check_AddDriveName Then 
                                        List_Lijst.AddItem _ 
                                           PATH_FILENAME & " ==>[ " & FileString & " ]" 
                                     Else 
                                        List_Lijst.AddItem _ 
                                           Mid(PATH_FILENAME, InStr(PATH_FILENAME, "\") + 1) & _ 
                                           " ==>[ " & FileString & " ]" 
                                     End If 
                                  Else 
    '&#167; only filename 
                                     List_Lijst.AddItem _ 
                                        FileName & " ==>[ " & FileString & " ]" 
                                     List_Lijst.Refresh 
                                  End If 
                               End If 
    '&#167; No filename? 
                           Else 
                               List_Lijst.AddItem "Got file: [No name]" 
                           End If 
    '&#167; Work out how much extra data to skip over 
                           SeekSize = .zlfhCompressedSize 
                           If (.zlfhExtraFieldLength) Then _ 
                               SeekSize = SeekSize + .zlfhExtraFieldLength 
                           If (.zlfhBitFlag And &H4) Then _ 
                               SeekSize = SeekSize + 12 
    '&#167; Seek to next record 
                           Seek #FNum, Seek(FNum) + SeekSize 
                      End With 
    '&#167; Increment file count 
                   Else 
                       Exit Do 
                   End If 
                Loop 
             Close #FNum 
          Else 
             MsgBox ("Error in ZIP filename: " & PATH_FILENAME) 
          End If 
       End If 
    Exit Function 
    Error_in_File: 
       MsgBox ("There is an error reading file: " & PATH_FILENAME) 
       List_Lijst.AddItem FileName & " ERROR READING FILE " 
    End Function
    Last edited by WizBang; October 13th, 2009 at 04:36 PM. Reason: Added [code] tags

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: how to trap an error reading CDrom

    Perhaps you can replace the line
    Code:
    On Error GoTo Error_in_File
    with
    Code:
    If Err.Number Then
      'your error code here
    End If
    Besides, using GoTo to skip out of a Do...Loop is a bad idea.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Oct 2009
    Posts
    12

    Re: how to trap an error reading CDrom

    thanks WizBang for Your help. all works fine now.

    br, ggeu

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center