0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LINUXの階層パスから全パスに変更するVBA

Last updated at Posted at 2025-02-19
Attribute VB_Name = "LinuxDirParser"
' Linuxディレクトリ構造解析モジュール
' 入力ファイルの形式を解析し、階層構造をTAB区切りで出力する

Option Explicit

' メイン処理関数
Public Sub ParseLinuxDirectory()
    On Error GoTo ErrorHandler
    
    Dim fso As Object
    Dim inputFile As Object
    Dim outputFile As Object
    Dim filePath As String
    Dim outputPath As String
    Dim currentLine As String
    Dim indentLevel As Integer
    Dim parentDirs() As String
    Dim i As Long
    
    ' ファイルパスの設定
    filePath = ThisWorkbook.Path & "\input.log"  ' 入力ファイルパス
    outputPath = ThisWorkbook.Path & "\output2.txt"  ' 出力ファイルパス
    
    ' オブジェクト初期化
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' UTF-8ファイル読み込み用ストリーム
    Dim stream: Set stream = CreateObject("ADODB.Stream")
    With stream
        .Charset = "utf-8"
        .Open
        .LoadFromFile filePath
    End With
    
    ' 出力ファイル作成
    Set outputFile = fso.CreateTextFile(outputPath, True, True)
    
    ' 階層管理用配列
    ReDim parentDirs(0 To 0)
    
    ' ファイル処理ループ
    ' テキスト全体を読み込みLFで分割(Linux形式対応)
    Dim allText As String
    allText = stream.ReadText
    Dim lines As Variant
    lines = Split(allText, vbLf)
    
    Dim lineIndex As Long
    For lineIndex = LBound(lines) To UBound(lines)
        currentLine = Trim(lines(lineIndex))
        
        ' 空行スキップ(コメント付加)
        If currentLine = "" Then GoTo ContinueLoop  ' 空行をスキップ
        
        ' 階層レベル計算
        indentLevel = CalculateIndentLevel(currentLine)
        
        ' パス要素抽出
        Dim pathElement As String
        pathElement = ExtractPathElement(currentLine)
        
        ' 階層構造更新
        If indentLevel > UBound(parentDirs) Then
            ReDim Preserve parentDirs(0 To indentLevel)
        Else
            ReDim Preserve parentDirs(0 To indentLevel)
        End If
        parentDirs(indentLevel) = pathElement
        
        ' 出力行生成
        Dim outputLine As String
        outputLine = BuildOutputLine(parentDirs, indentLevel)
        
        ' ファイル書き込み
        outputFile.WriteLine outputLine
        
ContinueLoop:
    Loop
    
    ' 後処理
    stream.Close
    outputFile.Close
    MsgBox "処理が完了しました", vbInformation
    Exit Sub
    
ErrorHandler:
    MsgBox "エラーが発生しました:" & vbCrLf & Err.Description, vbCritical
End Sub

' インデントレベル計算関数(修正版)
Private Function CalculateIndentLevel(line As String) As Integer
    Dim i As Integer
    Dim count As Integer
    count = 0
    
    ' "|"の数をカウントして階層を判定(先頭の「|--」分を除外)
    For i = 1 To Len(line)
        If Mid(line, i, 1) = "|" Then
            count = count + 1
        End If
    Next i
    
    ' ルート要素はレベル0
    CalculateIndentLevel = count - 1
End Function

' パス要素抽出関数(修正版)
Private Function ExtractPathElement(line As String) As String
    Dim pos As Integer
    pos = InStrRev(line, "|") + 1
    ExtractPathElement = Trim(Mid(line, pos))
    
    ' 「--」が含まれる場合のクリーニング
    If Left(ExtractPathElement, 2) = "--" Then
        ExtractPathElement = Mid(ExtractPathElement, 3)
    End If
End Function

' 出力行生成関数
Private Function BuildOutputLine(parents() As String, level As Integer) As String
    Dim result As String
    Dim i As Integer
    
    For i = 0 To level
        If i > 0 Then
            result = result & vbTab
        End If
        result = result & parents(i)
    Next i
    
    BuildOutputLine = result
End Function



```v
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?