execCmd = "%comspec% /c nslookup.exe " ' Leave a space before the last quote
sourceFile = "c:\comp.txt"
targetFile = "c:\ip.txt"
forReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(sourceFile,forReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Set WshShell = WScript.CreateObject("WScript.Shell")
RetVal = WshShell.Run(execCmd & strLine & " >> " & targetfile , 1, True)
Loop
set WshShell = nothing
set objFSO = nothing
Instead of writing the output to targetfile, I'd like to get the StdOut info
and parse it so I only write the last two lines to targetfile.
Basically, I'm not getting how to use StdOut to store the output from the
command in a way that I can parse.
Hi Chris,
[...]
| Instead of writing the output to targetfile, I'd like to get the
| StdOut info and parse it so I only write the last two lines to
| targetfile.
Below an example of script to capture the stdout stream of the external
command nslookup. The NSLookup function will return the last two lines
in an array.
-- cut here --
Option Explicit
Dim arrRet
arrRet = NSLookup ("192.168.40.130")
WScript.Echo arrRet (0) & VbCrLf & arrRet (1)
Function NSLookup (strIPAddress)
Dim oSh, oEx
Dim strLine, arrLastTwoLines (1)
Set oSh = CreateObject ("WScript.Shell")
' execute the external command
Set oEx = oSh.Exec ("%comspec% /c nslookup " & strIPAddress)
' wait for the command to complete
Do While oEx.Status = 0
wScript.Sleep (10)
Loop
' read the output stream
Do While Not oEx.Stdout.AtEndOfStream
strLine = Trim (oEx.Stdout.Readline)
If Left (strLine, 5) = "Name:" Then
' the IP address was resolved
arrLastTwoLines (0) = strLine
arrLastTwoLines (1) = oEx.Stdout.Readline
Exit Do
End If
Loop
NSLookup = arrLastTwoLines
End Function
-- cut here --
Hope this helps. Let us know
--
Gilles LAURENT
Me contacter : http://cerbermail.com/?zoTY7ZkLcD
Option Explicit
Dim arrRet
' open the file that contains hostnames
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\comp.txt",1)
Do Until objFile.AtEndOfStream
strknox = objFile.ReadLine
arrRet = NSLookup (strknox)
' write to a file the ouput of the function
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO2.OpenTextFile("c:\ip.txt",8)
objFile2.WriteLine(arrRet(0))
objFile2.WriteLine(arrRet(1) & VbCrLf)
Loop
Function NSLookup (strknox)
Dim oSh, oEx
Dim strLine, arrLastTwoLines (1)
Set oSh = CreateObject ("WScript.Shell")
' execute the external command
Set oEx = oSh.Exec ("%comspec% /c nslookup " & strknox)
' wait for the command to complete
Do While oEx.Status = 0
wScript.Sleep (10)
Loop
' read the output stream
Do While Not oEx.Stdout.AtEndOfStream
strLine = Trim (oEx.Stdout.Readline)
If Left (strLine, 5) = "Name:" Then
' the IP address was resolved
arrLastTwoLines (0) = strLine
arrLastTwoLines (1) = oEx.Stdout.Readline
Exit Do
End If
Loop
NSLookup = arrLastTwoLines
End Function
[...]
| ' write to a file the ouput of the function
| Set objFSO2 = CreateObject("Scripting.FileSystemObject")
| Set objFile2 = objFSO2.OpenTextFile("c:\ip.txt",8)
| objFile2.WriteLine(arrRet(0))
| objFile2.WriteLine(arrRet(1) & VbCrLf)
objFile2.Close
It is mandatory to close the file because you open it in the loop for
each host names. Another solution is to open the file only once before
to enter in the loop and to close it when the process is completed :
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\comp.txt",1)
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO2.OpenTextFile("c:\ip.txt",8)
Do Until objFile.AtEndOfStream
strknox = objFile.ReadLine
arrRet = NSLookup (strknox)
objFile2.WriteLine(arrRet(0))
objFile2.WriteLine(arrRet(1) & VbCrLf)
Loop
objFile.Close
objFile2.Close
Hope this helps. Let us know.