Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2450
  • Last Modified:

VBA - ping.exe command within shell() method

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
msaccess
Asked:
msaccess
  • 5
  • 4
  • 2
  • +1
1 Solution
 
cymbolicCommented:
Try using an 8.3 naming convention for your output file.  DOS does not support long file names (Oh how quickly we forget!)
0
 
guillemsCommented:
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
 
msaccessAuthor Commented:
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
guillemsCommented:
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
 
cymbolicCommented:
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
 
TrygveCommented:
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
 
cymbolicCommented:
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
 
cymbolicCommented:
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
 
msaccessAuthor Commented:
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
 
guillemsCommented:
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
 
cymbolicCommented:
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
 
guillemsCommented:
msaccess, eoo!!! are you here???, I expect you give me the points or reject my answer, but do something. Please

Thanks.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now