Solved

Error Handling A Text File

Posted on 2004-09-23
2
144 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
2 Comments
 
LVL 4

Accepted Solution

by:
avi247 earned 250 total points
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now