Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ping computers, extracting data from text files

Status
Not open for further replies.

techsupport3977

Technical User
Mar 7, 2005
56
US
I need to know if I am on the right path to extracting data from this code.

I was guided to create the code below, but I am a novice poweruser that is eager to get this done. I need to ping a computer, write the text file and then open the text file, read the text file and insert the data into a table in access.


Code:
Private Sub Form_Open(Cancel As Integer)

'Ping a computer and save it to a text file
Shell "cmd.exe /c ping XX.XX.XX.XX > c:\temp\ping\extruder_e.txt"

'Open the file for data extraction
Dim myString As String
Dim strArray() As String

Open "c:\temp\ping\extruder_e.txt" For Input As #1
    strArray = Split(Input(LOF(1), 1), vbCrLf)
    
Close #1
End Sub
 
See faq222-2244 for reasons not to cross-post threads. If you would go to your other thread and mention that you've reposted to here, it will help everyone keep the answers straight.

As for your code, it looks ok to me at a glance. You might want to look into the FileSystemObject, etc. in Microsoft Scripting Runtime, which I'm better at using personally, so I might be biased. Anyway, does the code that you have show what you want in strArray? If so, what do you need help with specifically?

Bob
 
The problem I see with this process is that when you hit the shell command it might take a while to execute the ping. This means that the text file will not exist and will probably not be complete when you hit the code to open it.

It would benefit you to do a search on ShellAndWait in this forum. This will help you get around the issue I described above.

Perhaps you could enligten us with what your end result is (like what the point in having the ping results in a access database is). There may be a better way altogether to accomplish what you want...
 
You can achieve the same effect (the array of strings) in about one line of code and without having the interim text file ...
 
Yeah, that does lead to the question, why do you need to have a text file as well as having the table in Access, techsupport? I was assuming you had business reasons to do so.

Bob
 
I suspect that the reason for the text file is that who ever advised on the solution didn't know of any other way of VB getting hold of the output from a command line program
 
That would be one way, yes ...

Here's the short way (that duplicates the OP version functionality):
Code:
[blue]Private Function GetCommandOutput(strCommand As String) As String()
    GetCommandOutput = Split(CreateObject("WScript.Shell").Exec(strCommand).StdOut.ReadAll, vbCrLf)
End Function
[/blue]
 
strongm,
I just happened to stumble across this thread and my immediate reaction was to take offense with

I suspect that the reason for the text file is that who ever advised on the solution didn't know of any other way of VB getting hold of the output from a command line program

I first responded to this one at thread181-1102870
in the access groups. My post was only intended to be what I preceived to be the simplest solution at the time

Being a frequent Tek-Tips visitor I was aware of how to use the Microsoft Scripting Runtime Object Library to return the value but I preceived it to be rather difficult.

After a look at your 1 liner my feelings are no longer hurt. Of course I am adding it to my toolbox and claiming it as mine own :)

SWEET



 
Perhaps we can look for all of the places that techsupport has posted this, so we can replicate strongm's answer to all of them and make sure he sees it. >:|

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top