【问题标题】:How to search for a variable in a text file using VBS如何使用 VBS 在文本文件中搜索变量
【发布时间】:2012-11-15 09:38:47
【问题描述】:

在我正在编写的一段代码方面需要帮助。我是脚本新手,请见谅。

基本上在下面的代码中,每次找到文本“Busy working Thread Id=”时,intBusyThreads 都会增加 1。

Do While oTextFile.AtEndOfStream <> True
  strLine = oTextFile.ReadLine
  If inStr(strLine, "Busy Working Thread Id=") Then
    intBusyThreads = intBusyThreads + 1
  End If
Loop

但是我只希望它在线程 ID 不同时增加。

例如:

繁忙的工作线程 ID=0x01
繁忙的工作线程 ID=0x05
繁忙的工作线程 ID=0x01

在此示例中,我的代码只会将 intBusyThreads 增加 2,因此线程 ID 01 和 05 是唯一的。我在试图找到允许我这样做的代码时遇到了麻烦。由于线程 ID 总是会改变,我不知道如何指定该变量。 (希望这是有道理的)我尝试了一些东西,比如strcomp()。有什么建议吗?

谢谢!

【问题讨论】:

    标签: scripting vbscript wsh


    【解决方案1】:

    使用唯一的线程 ID 创建一个 dictionary,并仅在当前 ID 不存在于字典中时才增加计数器:

    Set uniqueIDs = CreateObject("Scripting.Dictionary")
    
    Do While oTextFile.AtEndOfStream <> True
      strLine = oTextFile.ReadLine
      If inStr(strLine, "Busy Working Thread Id=") Then
        id = Split(strLine, "=")(1)
        If Not uniqueIDs.Exists(id) Then
          intBusyThreads = intBusyThreads + 1
          uniqueIDs.Add id, True
        End If
      End If
    Loop 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      相关资源
      最近更新 更多