Solved

VBA - ping.exe command within shell() method

Posted on 1998-02-19
12
2,225 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
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 9

Expert Comment

by:cymbolic
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Accepted Solution

by:
guillems earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Expert Comment

by:cymbolic
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
msaccess, eoo!!! are you here???, I expect you give me the points or reject my answer, but do something. Please

Thanks.
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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

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

16 Experts available now in Live!

Get 1:1 Help Now