Solved

VBA - ping.exe command within shell() method

Posted on 1998-02-19
12
2,253 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
 
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Contents of subform based upon combobox value 3 26
Tags from access to excel 3 30
Excess tables to Excel BackUp 3 29
format date field on certain entries 8 29
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

895 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

14 Experts available now in Live!

Get 1:1 Help Now