?
Solved

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

Posted on 2006-11-06
6
Medium Priority
?
338 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

649 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