对于每个带计数器的循环
for each loop with counter
我在 VBScript 中有一个 For Each
循环,它遍历文件和 returns 结果。
密码是:
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
我想我需要调整循环并实现计数器来打印迭代次数。请提出建议。
您将信息放在两个不同的地方(path.innerHtml
和 xmldoc.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
我在 VBScript 中有一个 For Each
循环,它遍历文件和 returns 结果。
密码是:
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
我想我需要调整循环并实现计数器来打印迭代次数。请提出建议。
您将信息放在两个不同的地方(path.innerHtml
和 xmldoc.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