CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 1999
    Location
    USA
    Posts
    101

    Need some help with error handling

    I have an application that opens several files.
    It has to check if that file resides in the specified directory.
    Also it has to check if there is any data in the file.
    If not, it has to rise approprite messages. How can I accomplish this?

    thanks for any suggestions


  2. #2
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: Need some help with error handling

    One solution would be to use the Dir Function. This function returns the filename if the file exists, and an empty string if it does not.

    Dim sFileName as string
    sFileName = Dir("C:\Windows\Desktop\MyFile.txt")
    If sFileName = vbNullString then
    'The file does not exist, raise an error
    Err.Raise ERROR_NO_FILE 'Define this error number
    else
    'Open the file
    'Depending on your choice for access
    'Check for EOF and BOF and set a boolean
    'to indicate no data
    If bNoData then
    Err.Raise ERROR_NO_DATA
    else
    'Read and process the file
    End If
    'Close the file
    End If





  3. #3
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Need some help with error handling

    I would suggest using the FileSystemObject:

    Sub Main()
    'set a reference to the Microsoft Scripting Runtime
    Dim fso as scripting.filesystemobject
    dim filer as scripting.textstream

    set fso = new scripting.filesystemobject

    if fso.fileexists("C:\yourfile.txt") then
    set filer = fso.opentextfile("C:\yourfile.txt",ForReading,false)
    else
    msgbox "File does not exist."
    goto ExitRoute
    end if

    if filer.atendofstream then
    msgbox "File is empty."
    end if

    ExitRoute:
    on error resume next
    filer.close
    set filer = nothing
    set fso = nothing
    End Sub




    That should do it for you.

    John

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

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

Featured