Solved

Why wont each address be echoed to the file ?

Posted on 2004-09-13
6
170 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 100 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

786 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