如何在Vbscript的Exists方法字典中使用值项而不是键进行搜索?

3
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")

谢谢,这对我来说意义重大! - niku
1个回答

4

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

编辑:2020年8月13日@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

编辑:2020年8月13日@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

1
你可以进一步将其作为可重复使用的函数而不仅仅是一次性脚本,例如 ExistsValue(d, Capital2Search)。不过,回答很好。 - user692942
嘿,我想在从查询中提取对象的逻辑中使用这个方法来获取WMI服务,并将它们放入字典中。如果服务与字典中的匹配项相符,则应该打印出来。你能帮我举个例子吗?以下是我用来提取对象的代码: Set colSWbemObjectSet = objSWbemServices.ExecQuery _ ("SELECT * FROM Win32_Service") - niku
@NikshepAKulli 请检查我最后一次修改的代码,并在这种情况下不要忘记将其标记为答案! - Hackoo
@Hackoo,我发了一个新的问题,请看一下:https://stackoverflow.com/questions/63774694/i-want-to-use-wildcards-to-compare-strings-in-second-method-service2search - niku
请您分享一下对此的想法。 - niku
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接