[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I capture null result or error message from a Shell command?

Posted on 2006-11-06
6
Medium Priority
?
347 Views
Last Modified: 2010-04-30
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
0
Comment
Question by:tilacakr
  • 3
  • 3
6 Comments
 

Author Comment

by:tilacakr
ID: 17886147
I forgot to mention that there are some functions being called that are not part of the code I posted before. They have nothing to do with the physical output.
0
 
LVL 2

Accepted Solution

by:
eheimer earned 750 total points
ID: 17886640
I think the problem is that the command "nslookup 192.168.0.1 >> outfile.txt" only redirects stdout to the file.  In the event that nslookup throws an error, that gets written to stderr.  The good news is that with a command you can redirect both stderr AND stdout, but the bad news is that you can't send them to the same file.  The following command redirects stderr to one file and stdout to a different file:

nslookup 192.168.0.1 2>stderr.txt 1>stdout.txt

You could do something like this within your loop:

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)

What this will do is send stderr to one file (overwriting the contents) and stdout to another file (also overwriting).  The next two lines concatenate the contents of those two files appending to your main output file.  You could also open the files up within VBA, read the contents and output to your main output file if you didn't want to use the last two shell commands.
0
 

Author Comment

by:tilacakr
ID: 17890072
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:eheimer
ID: 17891980
That should do the trick.  Let me know if you need any more help.
0
 

Author Comment

by:tilacakr
ID: 17892993
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.
0
 
LVL 2

Expert Comment

by:eheimer
ID: 17893183
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…
Suggested Courses

834 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