对于每个带计数器的循环

时间:2016-03-05 20:05:32

标签: xml xpath vbscript hta

我在VBScript中有一个For Each循环,它遍历文件并返回结果。

代码是:

Sub IterateSearch(FolderPath)
  On Error Resume Next
  Set fldr = fso.GetFolder(FolderPath)

  Set Fls = fldr.files
  For Each thing in Fls
    sFSpec = FSO.GetAbsolutePathName(thing)
    objMSXML.async = True
    objMSXML.load sFSpec
    If 0 = objMSXML.parseError Then
      Dim sXPath : sXPath = "//*[local-name()='namespace']/*[local-name()='querySubject']/*[local-name()='queryItem'][contains(., '"& searchTerm &"')]/ancestor-or-self::*/*[local-name()='name' and @locale='en']"

      Dim querySubject : Set querySubject = objMSXML.selectSingleNode(sXPath)
      path.innerHtml = path.innerHtml & thing.path &"<br>"
      If querySubject Is Nothing Then
        MsgBox sXPath, "failed"
      Else
        For Each node In objMSXML.selectNodes(sXPath)
          xmldoc.innerHtml = xmldoc.innerHtml & node.text & " " & "<br>" 
'         ObjOutFile.WriteLine Linenum & "  " & thing.path
        Next
'       xmldoc.innerHtml = xmldoc.innerHtml & "<br><br>"
      End If
    Else
      MsgBox objMSXML.parseError.reason
    End If
  Next

  Set fldrs = fldr.subfolders
  For Each thing in fldrs
    IterateSearch thing.path
  Next
End Sub

如果有多个结果,则打印为:

> - File Path 1
> - File Path 2
> - File Paht 3

 - result set 1
 - result set 2
 - result set 3

我想以这样的方式实现它,结果应该打印如下:

>   1. File Path 1
 - Result Set 1
>   2. File Path 2
 - Result Set 2
>   3. File Paht 3
 - Result Set 3

我认为我需要调整循环并实现计数器来打印迭代次数。请建议。

1 个答案:

答案 0 :(得分:1)

您将信息放在两个不同的位置(path.innerHtmlxmldoc.innerHtml)。如果您希望按照处理顺序显示它们,则需要创建新元素并将其附加到HTML正文中,例如:像这样:

Set p = document.createElement("p")
p.innerText = thing.Path
document.body.appendChild p

Set ul = document.CreateElement("ul")
For Each node In objMSXML.selectNodes(sXPath)
  Set li = document.createElement("li")
  li.innerText = node.text
  ul.appendChild li
Next
document.body.appendChild ul