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

x
?
Solved

Why wont each address be echoed to the file ?

Posted on 2004-09-13
6
Medium Priority
?
177 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

885 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