Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why wont each address be echoed to the file ?

Posted on 2004-09-13
6
Medium Priority
?
175 Views
Last Modified: 2012-05-05
Scenario
1 flat input file contains ip addresses :
190.1.1.1.
172.24.212.1
190.1.1.2

2 audit files

todo: read each ip address in and attempt ping. Log result into a log file containing current address then write contents out to a log file containing all entries .

What i get is all addresses wrtitten out to control file out.txt but only the result of the last ping to the control file pinglog.txt which would assume, the shell command is only being passed once.

Private Sub Command1_Click()

Dim objname As String

Open "C:\wip\adlist.txt" For Input As #1
Open "C:\wip\out.txt" For Output As #2
Open "C:\wip\pinglog.txt" For Output As #3


Do While Not EOF(1)
     
Input #1, objname

Print #2, objname

retval = Shell("cmd /c c:\windows\system32\ping.exe " & objname & " -n 10  >> c:\wip\pinglog.txt", 0)

Loop

Close #1
Close #2
Close #3

End Sub
0
Comment
Question by:123dec45
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 12045284
Hi 123dec45,
First of all, this line
Open "C:\wip\pinglog.txt" For Output As #3
isn't even needed in your code.

You also have a trailing period after your first ip address (maybe a type-o)

Try

Dim objname As String
Open "C:\wip\adlist.txt" For Input As #1
Open "C:\wip\out.txt" For APPEND As #2

Do While Not EOF(1)
 Input #1, objname
 Print #2, objname
 Shell ("cmd /c ping.exe " & objname & " -n 10  >> c:\wip\pinglog.txt")
Loop
0
 
LVL 1

Author Comment

by:123dec45
ID: 12045440
hmm nearly;-
yep it was a type-o !
i have 3 enteries in my adlist file
i get 3 records in my out.txt :-)
i get only 1 entry in my pinglog :-(
I have manualy tested all 3 entries nad they reply fine, I have also reduced the -n to 1 to get a quicker response. Interestingly, to see the data written to out.txt you have to halt the code. I wonder if there is something stuck that is not being flushed between each Shell command ?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 12045547
It's probably running over itself - try using shellwait instead:

Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400

Private Function ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbNormalFocus) As Double
Dim hProcess As Long, RetVal As Long
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(PathName, WindowStyle))
    Do
        GetExitCodeProcess hProcess, RetVal
        DoEvents: Sleep 100
    Loop While RetVal = STILL_ACTIVE
End Function

Shellwait ("cmd /c ping.exe" & objname & " -n 1 >> c:\wip\pinglog.txt")
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:123dec45
ID: 12052189
yep i think your right, i also came accross this whilst on the train last night trying some DIR's. When I put a sleep for 5 seconds between each loop, the problem went away. That said I am not certain a pause for x seconds is the best way. The example I give I am doing a fairly simple pings, in real life it will run some fairly long sub routines depending on the result of the ping, with complex routing it could take some time to learn a new route over several hops. Maybe a better way could be a trigger, either writing to the registry or a file to show the loop starting and stopping ? I have 3500 machines to diagnose so any seconds I can save will make a real difference. Thanks for any other suggestions you have. Regards
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 400 total points
ID: 12052592
The shellwait should do it for you then.  It will run the the command and wait for it to complete, regardless of how short or long it takes...
0
 
LVL 1

Author Comment

by:123dec45
ID: 12076955
that worked fine,
thanks for your help :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

670 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