Solved

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

Posted on 2006-11-06
6
332 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

738 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