Solved

VBA - ping.exe command within shell() method

Posted on 1998-02-19
12
2,276 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
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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.

809 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