tilacakr
asked on
How do I capture null result or error message from a Shell command?
We are using a button in MS Access to run the VBA code below to run "NSLOOKUP" against many IP addresses in the command shell and print what is returned to a text file. The only problem is that when nothing is returned, we can't capture that. We need to know when nothing (or an error message) is returned by NSLOOKUP. When we run NSLOOKUP manually, we do get an error message on screen, but it is not captured by the code below.
If you can easily modify our existing code to fix this, that would be best as I'm a novice.
Thx,
Tim
Private Sub Command0_Click()
Dim ff As Integer
Dim x As Integer
Dim outputfile As String
outputfile = "c:\IPOutput.txt"
ff = FreeFile
Open "c:\IPHostname.txt" For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
PID = Shell(Environ$("COMSPEC") & " /c nslookup " & Ln & " >> " & outputfile)
If PID = 0 Then
MsgBox "Shell command didn't work", vbOKOnly
Else
hProcess = OpenProcess(&H100000, True, PID)
WaitForSingleObject hProcess, -1
CloseHandle hProcess
End If
DoEvents
Loop
Close #ff
If you can easily modify our existing code to fix this, that would be best as I'm a novice.
Thx,
Tim
Private Sub Command0_Click()
Dim ff As Integer
Dim x As Integer
Dim outputfile As String
outputfile = "c:\IPOutput.txt"
ff = FreeFile
Open "c:\IPHostname.txt" For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
PID = Shell(Environ$("COMSPEC") & " /c nslookup " & Ln & " >> " & outputfile)
If PID = 0 Then
MsgBox "Shell command didn't work", vbOKOnly
Else
hProcess = OpenProcess(&H100000, True, PID)
WaitForSingleObject hProcess, -1
CloseHandle hProcess
End If
DoEvents
Loop
Close #ff
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here is the code with the lines you suggested added and new variables declared. Let me know if this looks correct.
Private Sub Command0_Click()
Dim ff As Integer
Dim x As Integer
Dim outputfile As String
Dim stderrfile As String
Dim stdoutfile AS String
outputfile = "C:\IPOutput.txt"
stderrfile = "C:\BadIP.txt"
stdoutfile = "C:\GoodIP.txt"
ff = FreeFile
Open "c:\IPHostname.txt" For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
PID = Shell(Environ$("COMSPEC") & " /c nslookup " & Ln & " 2> " & stderrfile & "1> " & stdoutfile)
PID = Shell(Environ$("COMSPEC") & " /c type " & stderrfile & " >> " & outputfile)
PID = Shell(Environ$("COMSPEC") & " /c type " & stdoutfile & " >> " & outputfile)
If PID = 0 Then
MsgBox "Shell command didn't work", vbOKOnly
Else
hProcess = OpenProcess(&H100000, True, PID)
WaitForSingleObject hProcess, -1
CloseHandle hProcess
End If
DoEvents
Loop
Close #ff
End Sub
Private Sub Command0_Click()
Dim ff As Integer
Dim x As Integer
Dim outputfile As String
Dim stderrfile As String
Dim stdoutfile AS String
outputfile = "C:\IPOutput.txt"
stderrfile = "C:\BadIP.txt"
stdoutfile = "C:\GoodIP.txt"
ff = FreeFile
Open "c:\IPHostname.txt" For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
PID = Shell(Environ$("COMSPEC") & " /c nslookup " & Ln & " 2> " & stderrfile & "1> " & stdoutfile)
PID = Shell(Environ$("COMSPEC") & " /c type " & stderrfile & " >> " & outputfile)
PID = Shell(Environ$("COMSPEC") & " /c type " & stdoutfile & " >> " & outputfile)
If PID = 0 Then
MsgBox "Shell command didn't work", vbOKOnly
Else
hProcess = OpenProcess(&H100000, True, PID)
WaitForSingleObject hProcess, -1
CloseHandle hProcess
End If
DoEvents
Loop
Close #ff
End Sub
That should do the trick. Let me know if you need any more help.
ASKER
Well, it worked, but I had to add another > to each of the std output files and put the concatenation piece outside of the loop.
Can you tell me why there is not a way to put them together in the same file?
Thanks for the help in getting there.
Can you tell me why there is not a way to put them together in the same file?
Thanks for the help in getting there.
When you redirect both stout and stderr in the same command to the same file you get an error that the file is already open for editing. At least that was my experience. You could try sending them to the same file, but I imagine you would get the same error.
Glad you got it working.
Glad you got it working.
ASKER