1

Possible Duplicate:
Vbscript - Read ini or text file for specific section

I am trying to check whether IPaddress of other PC can be accessed or not. If IPaddress is static, following code is ok.

vbscript code

Dim Shell, strCommand, strHost, ReturnCode
strHost = "192.168.10.1"
Set Shell = wscript.createObject("wscript.shell")
strCommand = "ping -n 1 -w 300 " & strHost
ReturnCode = Shell.Run(strCommand, 0, True)
If ReturnCode = 0 Then
wscript.echo strHost & " is pingable"
Else
wscript.echo strHost & " is not pingable"
End If

As I want to check dynamic IPaddress, use ini file.

ini file

[IPaddress]
IP001 = "192.168.10.1";
IP002 = "192.168.10.2"; 

Now, I would like to know how to connect ini file and vbscript code. Please explain it to me.

Community
  • 1
  • 1
eiphyo
  • 13
  • 2
  • 4
  • 1
    There is no built in functionality in VBScript for this - the only way is using plain FSO and parse the file yourself. – Shadow The GPT Wizard Sep 14 '11 at 07:07
  • A useful project for that: [INI Reader / Writer Class](http://www.codeproject.com/KB/files/VB_NET_INIFile_Object.aspx) – Kul-Tigin Sep 15 '11 at 03:14
  • Check [this answer](http://stackoverflow.com/questions/3491965/vbscript-read-ini-or-text-file-for-specific-section/7469754#7469754) it should handle what you need and is pretty flexible, better for the future if you'll have changes in the INI file.. – Shadow The GPT Wizard Sep 19 '11 at 10:50

1 Answers1

1

As Shadow Wizard mentions in his comment, you'll need to use FSO (FileSystemObject) to read the file. The sample code for OpenTextFile would probably be a good place to begin.

If you move your current vbscript code into a Sub that accepts one ip address as the argument you can just call that sub for each ip address in the file.

Maybe something like this (completely untested code):

Const ForReading = 1
Set MyFile = fso.OpenTextFile(FileName, ForReading)
Do While MyFile.AtEndOfStream <> True
    line = MyFile.ReadLine
    If Left(line, 2) = "IP" Then
        ipAddress = Replace(Replace(Right(line, 8), ";", ""), """", "") 
        YourSubThatPings ipAddress
    End If
Loop
MyFile.Close

The code that gets out the ipAddress from the line probably needs to be rewritten to be a bit more flexible though. And if the ini file contains a lot of other data maybe you need to add some code to skip to the correct section etc.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116