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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logging the automatic pinging of a series of IP addresses in a text file produces error

Status
Not open for further replies.

PSchubert

Technical User
Jun 6, 2006
50
0
0
AU
Hello,

I have the following line in a vb script:

Code:
wshShell.run("%comspec% /k ping -a -n 1 " & sText & " >> " & sLog)

where sText is a line in a text file that contains ip addresses, one per line, and sLog is the text file in which to write the results.
The code works only for the a single ip address; then it returns "The process cannot access the file because it is being used by another process." This error must pertain to the

Code:
& " >> " & sLog)

part of the line, because when I leave it off, all of the ping results are written to their respective command windows.

How can I automatically append each ping result to the end of the sLog file?

Here's the entire script:

Code:
    Option Explicit
    On Error Resume Next

    Dim wshShell
    Dim strHost
    Dim strCommand
    Dim strLine
    Dim oFSO, sFile, oFile, sText, sLog

    Set wshShell = wscript.createObject("wscript.shell")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sFile = "indexIPs.txt"
    sLog = "Log.txt"

    If oFSO.FileExists(sFile) Then
        Set oFile = oFSO.OpenTextFile(sFile, 1)
        Do While Not oFile.AtEndOfStream
        sText = oFile.ReadLine
        If Trim(sText) <> "" Then
            strCommand = sText
            wshShell.run("%comspec% /k ping -a -n 1 " & sText [red]& " >> " & sLog[/red])
[green]'            wshShell.run("%comspec% /k ping -a -n 1 " & sText) 'this works as explained above[/green]
        End If
        Loop
        oFile.Close
    Else
        WScript.Echo "File '" & sFile & "' was not found."
    End If

    If Err <> 0 Then
        wscript.echo "Error Type = " & Err.Description
    End If

    WScript.Quit

Thanks in advance!
 
I found the answer, and of course it's very simple.

The line that was giving me trouble:

Code:
wshShell.run("%comspec% /k ping -a -n 1 " & sText & " >> " & sLog)

should be:

Code:
wshShell.run("%comspec% [b]/c[/b] ping -a -n 1 " & sText & " >> " & sLog)

As cmd /? says:

/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains

All is well now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top