?
Solved

vb script Read and write to file

Posted on 2014-09-10
1
Medium Priority
?
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…
Introduction to Processes

777 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