windows下tree命令生成的文件解析为绝对路径的工具

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,下载路径:

windows下tree命令生成的文件解析为绝对路径的工具v2-VB文档类资源-CSDN下载  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值