2016-03-05 29 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