Solved

vb script Read and write to file

Posted on 2014-09-10
1
420 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 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

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 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