Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用VBScript读取ini文件?_Vbscript - Fatal编程技术网

如何使用VBScript读取ini文件?

如何使用VBScript读取ini文件?,vbscript,Vbscript,可能重复: 我正在检查其他电脑的IP地址是否可以访问。 如果IPaddress是静态的,则以下代码正常 vbscript代码 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.Ru

可能重复:

我正在检查其他电脑的IP地址是否可以访问。 如果IPaddress是静态的,则以下代码正常

vbscript代码

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
[IPaddress]
IP001 = "192.168.10.1";
IP002 = "192.168.10.2"; 
由于要检查动态IP地址,请使用ini文件

ini文件

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
[IPaddress]
IP001 = "192.168.10.1";
IP002 = "192.168.10.2"; 
现在,我想知道如何连接ini文件和vbscript代码。
请给我解释一下。

正如影子向导在他的评论中提到的,您需要使用(FileSystemObject)来读取文件。的示例代码可能是一个很好的开始

如果将当前vbscript代码移动到接受一个ip地址作为参数的中,则只需为文件中的每个ip地址调用该sub即可

可能是这样的(完全未经测试的代码):

Const ForReading=1
设置MyFile=fso.OpenTextFile(文件名,用于读取)
当MyFile.AtEndOfStream为True时执行此操作
line=MyFile.ReadLine
如果左(第2行)=“IP”,则
ipAddress=Replace(Replace(右(第8行),“;”,“),”“,”“,”“)
您的子标题的IP地址
如果结束
环
我的文件。关闭

中取出
ipAddress
的代码可能需要重新编写,以便更灵活一些。如果ini文件包含大量其他数据,您可能需要添加一些代码以跳到正确的部分等。

正如影子向导在其评论中提到的,您需要使用(FileSystemObject)来读取该文件。的示例代码可能是一个很好的开始

如果将当前vbscript代码移动到接受一个ip地址作为参数的中,则只需为文件中的每个ip地址调用该sub即可

可能是这样的(完全未经测试的代码):

Const ForReading=1
设置MyFile=fso.OpenTextFile(文件名,用于读取)
当MyFile.AtEndOfStream为True时执行此操作
line=MyFile.ReadLine
如果左(第2行)=“IP”,则
ipAddress=Replace(Replace(右(第8行),“;”,“),”“,”“,”“)
您的子标题的IP地址
如果结束
环
我的文件。关闭

中取出
ipAddress
的代码可能需要重新编写,以便更灵活一些。如果ini文件包含大量其他数据,您可能需要添加一些代码以跳到正确的部分等。

VBScript中没有内置的功能用于此操作-唯一的方法是使用普通FSO并自己解析文件。这是一个有用的项目:检查它是否可以处理您需要的内容,并且非常灵活,如果您在INI文件中有更改,则对未来更有利。VBScript中没有内置的功能用于此操作-唯一的方法是使用普通FSO并自己解析文件。这是一个有用的项目:检查它是否可以处理您需要的内容,并且非常灵活,如果您在INI文件中有更改,则对未来更有利。。