使用字典来存储您的表格数据,并使用递归函数来构建位置字符串。
演示脚本:
Dim dicData : Set dicData = CreateObject("Scripting.Dictionary")
' nId wstrName nParentId
dicData( 0) = Array("Managed computers" , 2 )
dicData( 1) = Array("Unassigned computers", NULL)
dicData( 2) = Array("Master Server" , NULL)
dicData( 3) = Array("pseudohosts" , NULL)
dicData( 5) = Array("Server 2" , 0 )
dicData( 8) = Array("Group100" , 5 )
dicData(10) = Array("Group22" , 5 )
dicData(11) = Array("Group47" , 5 )
dicData(13) = Array("Group33" , 5 )
dicData(14) = Array("Group39" , 5 )
dicData(15) = Array("Group11" , 5 )
Dim nId
For Each nId In dicData.Keys()
WScript.Echo Right(100 + nId, 2), buildLoc(dicData, nId, Array())
Next
Function buildLoc(dicData, nId, aTmp)
ReDim Preserve aTmp(UBound(aTmp) + 1)
aTmp(UBound(aTmp)) = dicData(nId)(0)
If IsNull(dicData(nId)(1)) Then
reverseArr aTmp
buildLoc = Join(aTmp, "/")
Else
buildLoc = buildLoc(dicData, dicData(nId)(1), aTmp)
End If
End Function
Sub reverseArr(aX)
Dim nUB : nUB = UBound(aX)
Dim nUB2 : nUB2 = nUB \ 2
Dim i, vt
For i = 0 To nUB2
vt = aX(i)
aX(i) = aX(nUB - i)
aX(nUB - i) = vt
Next
End Sub
输出:
00 Master Server/Managed computers
01 Unassigned computers
02 Master Server
03 pseudohosts
05 Master Server/Managed computers/Server 2
08 Master Server/Managed computers/Server 2/Group100
10 Master Server/Managed computers/Server 2/Group22
11 Master Server/Managed computers/Server 2/Group47
13 Master Server/Managed computers/Server 2/Group33
14 Master Server/Managed computers/Server 2/Group39
15 Master Server/Managed computers/Server 2/Group11