Solved

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

Posted on 2006-11-06
6
333 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Industry Leaders: 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

690 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