windows下tree命令解析为树形结构想变成绝对路径 网上没有找到 就自己试着用EXCEL写了一个
1,界面如下
1)把用命令生成的文件从第三行开始复制到A列
2)点击实行按钮
2,结果如下:
3,主要代码如下
Private Sub CommandButton1_Click()
Dim rowIndex As Long
Dim lastText As String
Dim fileName As String
Dim isFile As Boolean
startRow = 2
rowIndex = startRow
While Cells(rowIndex, 1).Value <> ""
fileNme = ""
'如果是初期行
If rowIndex = startRow Then
isFile = False
Cells(rowIndex, 2) = Replace(Cells(rowIndex, 1), ".", "\")
Cells(rowIndex, 3) = Replace(Cells(rowIndex, 1), ".", "\")
Else
'第二行及以下
Cells(rowIndex, 2) = GetParent(rowIndex, fileName, isFile)
Cells(rowIndex, 3) = fso.BuildPath(Cells(rowIndex, 2), fileName)
If Trim(fileName) = "" Then
Cells(rowIndex, 2) = ""
Cells(rowIndex, 3) = ""
End If
End If
If isFile = True Then
Cells(rowIndex, 4) = "是"
Else
Cells(rowIndex, 4) = "否"
End If
rowIndex = rowIndex + 1
Wend
MsgBox "End"
End Sub
Private Function GetParent(rowIndex As Long, ByRef fileNme As String, ByRef isFile As Boolean) As String
Dim currentText As String
Dim preText As String
currentText = Cells(rowIndex, 1)
Dim currentIndex As Long
Dim preIndex As Long
'取得当前行和前行 ─ 的出现位置
'如果当前行比前行小 代表为上一行的子目录
If InStr(1, currentText, "+---", vbBinaryCompare) > 0 Or _
InStr(1, currentText, "\---", vbBinaryCompare) > 0 Then
isFile = False
currentIndex = InStr(1, currentText, "+---", vbBinaryCompare)
If currentIndex = 0 Then
currentIndex = InStr(1, currentText, "\---", vbBinaryCompare)
End If
fileNme = Trim(Mid(currentText, currentIndex + 4))
Else
currentIndex = InStrRev(currentText, "|", -1, vbBinaryCompare)
fileNme = Trim(Mid(currentText, currentIndex + 1))
isFile = True
End If
If fileNme = "" Then
isFile = False
End If
Dim udex As Long
'如果当前行比前行大
'从前行往前查找 找到和它 ─ 的出现位置一致的行
For udex = rowIndex - 1 To startRow Step -1
preText = Cells(udex, 1)
If isFile = True Then
If InStr(1, preText, "+---", vbBinaryCompare) > 0 Or _
InStr(1, preText, "\---", vbBinaryCompare) > 0 Or _
udex = startRow Then
GetParent = Cells(udex, 3).Value
Exit Function
End If
Else
If udex = startRow Then
GetParent = Cells(udex, 3)
Exit Function
Else
If InStr(1, preText, "+---", vbBinaryCompare) > 0 Or _
InStr(1, preText, "\---", vbBinaryCompare) > 0 Then
preIndex = InStr(1, preText, "+---", vbBinaryCompare)
If preIndex = 0 Then
preIndex = InStr(1, preText, "\---", vbBinaryCompare)
End If
If preIndex < currentIndex Then
GetParent = Cells(udex, 3).Value
Exit Function
End If
End If
End If
End If
Next
End Function
4,下载路径: