Solved

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

Posted on 2006-11-06
6
323 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 250 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

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…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

13 Experts available now in Live!

Get 1:1 Help Now