dictionary - 如何使用 Vbscript 在 Exists 方法字典中使用值项而不是键进行搜索?
问题描述
Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
问题:我可以编写一个逻辑来检查d.Exists("Cario")
值而不是键d.Exists("c")
吗?
解决方案
Items 方法帮助我们获取存储在数据字典对象的键值对中的值。
object.Items( )
Option Explicit
Dim d,Capital,i,Capital2Search
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
'Items Method helps us to get the values stored in the key value pair of the data dictionary object.
'object.Items( )
Capital = d.items
For i=LBound(Capital) to UBound(Capital)
wscript.echo Capital(i)
Next
'Searching for Cairo
Capital2Search = "Cairo"
For i=LBound(Capital) to UBound(Capital)
If Instr(UCASE(Capital(i)),UCASE(Capital2Search)) > 0 Then
wscript.echo Capital2Search & " Exists ! "
End If
Next
编辑:13/08/2020 @18:00
请参阅您的最后一条评论:您可以执行以下操作:
Option Explicit
Dim Title : Title = "Find a Service by Name"
Dim Dico,objWMIService,colListOfServices,objService,Keys,ServiceName,Service2Search
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service")
Set Dico = CreateObject("Scripting.Dictionary")
' We fill our Dictionary in this loop For ... Next
For Each objService in colListOfServices
If Not dico.Exists(objService.Name) Then
Dico.Add objService.Name,objService.PathName
End If
Next
Service2Search = "Bits"
Keys = Dico.Keys
' Looking for a service name = "BITS" in this example :
For each ServiceName in Keys
If Instr(UCASE(ServiceName),UCASE(Service2Search)) > 0 Then
MsgBox "The service "& ServiceName & " : Exists !" & vbcrlf &_
"PahName : " & dico(ServiceName),vbInformation,Title
'Exit For
End If
Next
编辑:13/08/2020 @ 19:30
如果您想搜索一系列服务:
Option Explicit
Dim Title : Title = "Find a Service by Name into an Array"
Dim Dico,objWMIService,colListOfServices,objService,Keys
Dim ServiceName,Services,ArrService2Search,Service2Search,PathName
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service")
Set Dico = CreateObject("Scripting.Dictionary")
' We fill our Dictionary in this loop For ... Next
For Each objService in colListOfServices
If Not dico.Exists(objService.Name) Then
Dico.Add objService.Name,objService.PathName
End If
Next
ArrService2Search = Array(_
"Adobe",_
"Bits",_
"GoogleChromeElevationService",_
"gupdate",_
"gupdatem",_
"sedsvc",_
"SynTPEnhService"_
)
Services = Dico.Keys
' Looking for a service name in this array ArrService2Search :
For each Service2Search in ArrService2Search
For each ServiceName in Services
PathName = Dico(ServiceName)
If Instr(UCASE(ServiceName),UCASE(Service2Search)) > 0 Then
MsgBox "The service : " & chr(34) & ServiceName & chr(34) & " Exists !" & vbcrlf &_
"Path : "& chr(34) & PathName & chr(34),vbInformation,Title
End If
Next
Next
推荐阅读
- continuous-integration - 操作系统特定的 Puppet 单元测试
- javascript - 如何在 TypeScript 中键入 JSON 导入
- node.js - Azure Blob Node.js 下载 blob 文件
- r - 如何从 CSV 文件计算 R 中每部电影的平均评分?
- r - 如何在ggplot2中显示obs的方向(标题)
- python - 如何在 Pygame 上创建用户输入?
- mysql - 跨多个模式的 mySQL 查询
- angular - Mousevent方法不触发
- perl - 执行 shell 脚本 Sed 和 Perl:使用 & 作为匹配字符串
- python - 哪个是真正的 python3 可执行文件的路径?