Solved

VBA - ping.exe command within shell() method

Posted on 1998-02-19
12
2,337 Views
Last Modified: 2009-07-29
MS Access version 7 -- VBA code:

When I execute the following:

  PINGstring = "ping.exe hostname >Ping-Results.txt"
  ProgID = Shell(PINGstring, vbNormalFocus)

shell works ok by passing the string to DOS for execution.  However, the ping command fails with (as best as I can tell) a message indicating incorrect parameter (the redirection portion of the command).  

But, when I execute the exact same command when in the dos prompt window -- the command works perfectly.  How do I get this to work within VBA?

Actually, the more general question is:  I want to execute ping from VBA in order to see if the specified host is available or not.  So I want to somehow capture the output of ping for review in order to make that determination.  Seemed like executing ping, redirecting output to a temp file, reading the file, and serching for some keywords (like  "unreachable") would easily do the trick.  But redirection (>) within the shell() method doesn't seem to work???

thx in advance!!!
0
Comment
Question by:msaccess
[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
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 9

Expert Comment

by:cymbolic
ID: 1968818
Try using an 8.3 naming convention for your output file.  DOS does not support long file names (Oh how quickly we forget!)
0
 
LVL 3

Expert Comment

by:guillems
ID: 1968819
I've the solution:

  create a bat file like:

  ping 192.168.0.69  >p.txt

  the name of the .bat is <NAME>.bat

  then in the VBA code you must do:

Sub prova()
  Dim ProgID
  Dim PINGstring As String
 
  PINGstring = "<PATH>pepe.bat | command.com "
  ProgID = Shell(PINGstring, vbHide)  
End Sub

'--- Remember <PATH> is like "c:\temp\"

I try it and it works.

I hope that help you.
0
 

Author Comment

by:msaccess
ID: 1968820
What is the "8.3 naming convention"?

and in the comments addedd section ---
what is "command.com" supposed to be set to (or is it literal)


0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 3

Accepted Solution

by:
guillems earned 100 total points
ID: 1968821
If yuo type all the code, it must be run OK.

The command.com is the command.com of the system. It is a command.
(...)
Sub prova()
   Dim ProgID
   Dim PINGstring As String

  PINGstring = "<PATH><NAME>.bat | command.com " 
  ProgID = Shell(PINGstring, vbHide)
End Sub
(...)

This works well.

I call the <name>.bat concatenate to another program that is the command.com (of the DOS)

I hope this help you.
0
 
LVL 9

Expert Comment

by:cymbolic
ID: 1968822
Your original method should work if you pipe the output to a file name like "Ping.Txt", but a file name like you are using has a proper name portion greater than 8 characters.  DOS only works with 8 characters max for proper name, and three characters max for extension, that's the 8.3 naming convention under DOS.  Since you are shelling out, and issuing a DOS command line program, you must use the 8.3 naming convention.  DOS will not create the file name you are using because the proper portion of the file name is greater than 8 characters.
0
 
LVL 12

Expert Comment

by:Trygve
ID: 1968823
cymbolic: Naming convention is important, and the 8.3 will make a difference in DOS. You can use long filenames if you include them in quotation marks.

BUT: It seems like PING.EXE when run with the shell command looks upon > Filename as a parameter and does not know how to handle it. i.e. even ping.exe Host > Ping.txt will give you the error.

I think guillems' suggestion of using a stored bat file is the way of doing this. But there should be a more elegant way ...
0
 
LVL 9

Expert Comment

by:cymbolic
ID: 1968824
Not true on my 95 system. When I Ping a legitimate address IN THE WINDOWS DIRECTORY in  a DOS box and redirect to file x.x (who cares about the name, I'm gonna throw it away anyway) I get the results of the ping in the file.  Perhaps the problem is that his DOS pathing at shell time when shelled from his program can not find the ping.exe.  Try using full path name to shell ping.exe.
0
 
LVL 9

Expert Comment

by:cymbolic
ID: 1968825
But, I concur with guillems answer as a reliable way to invoke DOS commands in a shell.  Once you start processing a .bat file, you are guarenteed that the DOS command processor is in control.  Also, if you need flexibility, like modifying the address at run time, then simple write a one line .bat file containing your ping command, the ip address and your file piping, then use shell to execute the .bat file you just wrote.  One thing we know for sure, not everything works in 95 and NT/DOS just like it worked in native DOS, what a surprise!  
0
 

Author Comment

by:msaccess
ID: 1968826
I do use the full path name - that didn't help.
I also tried the 8.3 naming convention - that did not help.

I tried the .bat | command.com -- that worked BUT....

the ms dos window invoked to run ping does not seem to go away.  I accumulate processes that I cannot get rid of.  When I run ping (or ftp or telnet) from VBA and without involving command.com - once the command is completed, the ms dos screen automatically and cleanly goes away.  Apparently, there is something about command.com that changes this.
0
 
LVL 3

Expert Comment

by:guillems
ID: 1968827
I'm so glad, when I prove the program, I don't watch any DOS' Window, because I use a vbHIDE property.

ProgID = Shell(PINGstring, vbHide)

msaccess, tell me, I asked your question?

cymbolic, I agree with you, I didn't  think if a ping uses dinamyc adress you must delete the .bat, re-create the bat and execute. A lot of work. So ...
0
 
LVL 9

Expert Comment

by:cymbolic
ID: 1968828
If you want the DOS window to go away, as the last line of your .bat file, say EXIT.  This exits the command processor and the window, in the standard DOS window mode, which is to close window on exit.  Also, remember, you can use a command line parameter in your batch file, which could be your IP address, then your batch file would be:

@echo Off
ping %1>outfile.txt
Exit

And you would shell it as:

PINGIT.BAT 199.22.178.55

or whatever your file name is and your IP address is.

0
 
LVL 3

Expert Comment

by:guillems
ID: 1968829
msaccess, eoo!!! are you here???, I expect you give me the points or reject my answer, but do something. Please

Thanks.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

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