Solved

vb script Read and write to file

Posted on 2014-09-10
1
410 Views
Last Modified: 2014-09-22
hi

i write a little script that read from file base on "NODENAME" and "TCPSERVERADDRESS"
the TCPSERVERADDRESS will write base on ping success
 the node name section is work just fine
but the TCPSERVERADDRESS do nothing

Thx
David



Option Explicit
Dim strComputer
Dim objShell, objFSO , wshNetwork
Dim objInputFile, objOutputFile, strLine
Dim strTarget, strPingResults ,ping

strComputer = "."
Set objShell = Wscript.CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshNetwork = CreateObject("WScript.Network")
Set Ping = objShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs each
strTarget = "10.0.0.2" 'IP address or hostname
Set Ping = objShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs Each

Set objInputFile = objFSO.OpenTextFile("C:\Program Files\Tivoli\TSM\baclient\dsmTemp.opt", 1, True)
Set objOutputFile = objFSO.OpenTextFile("C:\Program Files\Tivoli\TSM\baclient\dsm.opt", 2, True)

Do Until objInputFile.AtEndOfStream
 strLine = objInputFile.ReadLine
 If Left(strLine, 8) = "NODENAME" Then
 strLine = Left(strLine, 8) & " " & WshNetwork.ComputerName
 objOutputFile.Writeline strLine
 Else
 objOutputFile.Writeline strLine
 End If
 

 
strPingResults = LCase(Ping.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
       If Left(strLine, 16) = "TCPSERVERADDRESS" Then
 strLine = Left(strLine, 16) & " " & "Test"
 objOutputFile.Writeline strLine
 Else
 objOutputFile.Writeline strLine
 End If
 End If
 
 'objInputFile.Close
 'objOutputFile.Close
 Loop
 
 'strPingResults = LCase(Ping.StdOut.ReadAll)
'If InStr(strPingResults, "reply from") Then
      'If Left(strLine, 17) = "TCPSERVERADDRESS" Then
            'strLine = Left(strLine, 16) & " " & WshNetwork.ComputerName
 'objOutputFile.Writeline strLine
 'Else
 'objOutputFile.Writeline strLine
 'End If
 'End If

'Loop
'objInputFile.Close
'objOutputFile.Close
'objFSO.DeleteFile "C:\Program Files\Tivoli\TSM\baclient\dsmTemp.opt", True
'============================================

'


WScript.Echo "Done!"
0
Comment
Question by:David Regev
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40314257
there are a couple of things in your script that seem incorrect...
Set Ping = objShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs each
strTarget = "10.0.0.2" 'IP address or hostname
Set Ping = objShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs Each

Open in new window

that 2nd set Ping = xxx will overwrite the first Ping object results... not sure if you really just want to run the first statement but discard it.

this line of code:
strPingResults = LCase(Ping.StdOut.ReadAll) 

Open in new window

is inside the loop, and should be BEFORE the loop, otherwise you will have issues on (trying to) reading the ping results over and over again.

I reworked the code a bit to optimize it
Option Explicit
Dim strComputer
Dim objShell, objFSO , wshNetwork
Dim objInputFile, objOutputFile, strLine
Dim strTarget, strPingResults ,ping

strComputer = "."
Set objShell = Wscript.CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshNetwork = CreateObject("WScript.Network")
Set Ping = objShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs each
strTarget = "10.0.0.2" 'IP address or hostname
Set Ping = objShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs Each

Set objInputFile = objFSO.OpenTextFile("C:\Program Files\Tivoli\TSM\baclient\dsmTemp.opt", 1, True)
Set objOutputFile = objFSO.OpenTextFile("C:\Program Files\Tivoli\TSM\baclient\dsm.opt", 2, True)

strPingResults = LCase(Ping.StdOut.ReadAll)


Do Until objInputFile.AtEndOfStream
 strLine = objInputFile.ReadLine
 If Left(strLine, 8) = "NODENAME" Then
     strLine = Left(strLine, 8) & " " & WshNetwork.ComputerName
 end if
 If Left(strLine, 16) = "TCPSERVERADDRESS" Then
     If InStr(strPingResults, "reply from") Then
          strLine = Left(strLine, 16) & " " & "Test"
      end if
 End If

 objOutputFile.Writeline strLine
 
 Loop
 
objInputFile.Close
objOutputFile.Close
'objFSO.DeleteFile "C:\Program Files\Tivoli\TSM\baclient\dsmTemp.opt", True
'============================================

WScript.Echo "Done!" 

Open in new window


note tested, but I think you see what the changes should do better than your code
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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

785 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