Solved

vb script Read and write to file

Posted on 2014-09-10
1
402 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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now