2016-03-05 76 views
0

我有一个在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

您将信息放在两个不同的地方(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