Solved

Error Handling A Text File

Posted on 2004-09-23
2
151 Views
Last Modified: 2010-05-02
I am working on a program that goes through a list of IP address (either from an imported text file or a list box) and retrieves information out of WMI then writes it to a CSV file.  The problem I am having is handling errors (access denied, machine doesn't exist, etc).

Let's say I have a list of 10 IP's and 3 of them are turned off.  So it goes through the first 3, retrieves the info and writes it to the CSV file.  The 4th is turned off.  The error needs to open that text file and write the IP and error description  on it's line.  After that, it goes to the 5th and 6th machines that work just fine.  But 7 and 8 are turned off, so they need to write the error to the text file (and so on and so forth).

Basically, I can't figure out how to write the error and return to my program using the next IP address in the list.

Thank you for your help.
0
Comment
Question by:daricsmith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
avi247 earned 250 total points
ID: 12138150
I am not sure what you are using, VB or Vb Script.

Here YourFunctionToCheckIpExists (IpAddress)  throws an error that is suppressed but checked for in the next line.

..you code for reading from file...
Open file for reading
While not end of file
- read ip from file
  On Error Resume Next
    YourFunctionToCheckIpExists (IpAddress)
   If Err.Number <> 0  then ' this means an error has been thrown above
     Select Case Err.Number
      Case -25374:
        ' write to file
        ' Err.Number gives the error number
        ' Err.Description gives the error description
     Case -394991:
        'write to file as before
   ......... and so on
 End Select
end if
Err.Clear
Go to next line of file
Wend
' close your file


When you give On Error Resume Next, no error box is thrown when an error is encountered. The control goes to the next line and Err.Number and Err.Descpriton get populted with error information.

In Select Case you can give Err.Description instead of Err.Number
 Select Case UCASE(Err.Description)
      Case "ACCESS DENIED":
         ........
     
  End Select

---------------------------------------------
Another method similar to above . Here you do the error suppression within your Ip Checking function.

Open file for reading
While not end of file
- read ip from file
   ReturnCode = YourFunctionToCheckIpExists (IpAddress)
   If ReturnCode  <> 0  then ' this means an error has been thrown above
     Select Case ReturnCode
      Case -25374:
        ' write to file
        ' Err.Number gives the error number
        ' Err.Description gives the error description
     Case -394991:
        'write to file as before
   ......... and so on
    End Select
   end if
Err.Clear
Go to next line of file
Wend
' close your file
end if

' We suppress any error the API throws, collect the error code and send back
Function YourFunctionToCheckIpExits( IpAddress)
 On error resume next
  ErrCode = Ping (IpAddress) ' Ping as an example, you might be doing somethin else.
  YourFunctionToCheckIpExits = ErrCode
End Function
0
 

Author Comment

by:daricsmith
ID: 12142897
I am using VB.  Here is my current code the first part of the 'IF' statement is checking if the LISTBOX is filled in.  If it is blank, it goes to the part where the user can import a text file and works from that:



Private Sub Command1_Click()
On Error Resume Next

Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\DIT Support Handle"
Kill "c:\output.csv"

'===================================================================
'This writes the header info to the .csv file
'===================================================================

Open "c:\output.csv" For Append As #3
Print #3, "Computer/IP" & ", " & "Name" & ", " & "Phone" & ", " & "Site" & ", " & "Building" & ", " & "Room" & ", " & _
"" & ", " & "Manufacturer" & ", " & "Model" & ", " & " Serial Number" & ", " & "Software" & _
", " & "Vendor" & ", " & "Vendor Contact" & ", " & "Vendor Phone" & ", " & "Vendor Email"
Close #3

If File_Field.Text = "" Then
   
    strGSKClass = "CXP_ManagedSystemv003"
    strComputer = List_Field.Text
   
    On Error GoTo Error
   
    '===================================================================
    'This connects to the remote computer's WMI Class
    '===================================================================
   
    Set objClass = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")
    Set colItems = objClass.ExecQuery("Select * from CXP_ManagedSystemv003")
   
    '===================================================================
    'This connects to the remote computer's Registry
    '===================================================================
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")
    oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, "DisplayName", arrValues

   
    '===================================================================
    'This reads the information from the remote computer and writes it to the .csv file.
    '===================================================================
    For Each objItem In colItems
        Open "c:\output.csv" For Append As #2
            Print #2, strComputer & ", " & objItem.CXP_PrimaryFirstName & " " & _
                objItem.CXP_PrimaryLastName & ", " & _
                objItem.CXP_PrimaryPhone & ", " & _
                objItem.CXP_Site & ", " & _
                objItem.CXP_BuildingLocation & ", " & _
                objItem.CXP_Room & ", " & _
                arrValues
               
            Close #2
    Next
Exit Sub
Error:
    Open "c:\output.csv" For Append As #2
        Print #2, strComputer & ", " & Err.Description
    Close #2
    Exit Sub
   
    Resume Next
   
Else

    Open (File_Field.Text) For Input As #1

   
    '===================================================================
    'This loops through the file list of computers and writes the info
    'To the .csv file for each system.
    '===================================================================
       
    While Not EOF(1)
        Line Input #1, strComputer
   
        strGSKClass = "CXP_ManagedSystemv003"
   
        Set objClass = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\cimv2")
        Set colItems = objClass.ExecQuery("Select * from CXP_ManagedSystemv003")
       
        '===================================================================
        'This connects to the remote computer's Registry
        '===================================================================
        Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\default:StdRegProv")
        oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, "DisplayName", arrValues
       
        For Each objItem In colItems
       
            Open "c:\output.csv" For Append As #2
            Print #2, objItem.CXP_PrimaryFirstName & " " & _
                objItem.CXP_PrimaryLastName & ", " & _
                objItem.CXP_PrimaryPhone & ", " & _
                objItem.CXP_Site & ", " & _
                objItem.CXP_BuildingLocation & ", " & _
                objItem.CXP_Room & ", " & _
                arrValues
            Close #2
   
        Next
       
    Wend
    Close #1
End If

Dialog.Show
Unload Me

End Sub



0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month4 days, 23 hours left to enroll

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question