0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ファイル階層から次の階層を見つける

Posted at
' Module to extract next level files/folders under specified path
Option Explicit

' ・皈、・Iタ・
Sub GetNextLevelItems()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim rootPath As String
    Dim searchPath As String
    Dim pathParts() As String
    Dim targetLevel As Long
    Dim dict As Object
    Dim resultStr As String
    
    ' Get input path (e.g. "/boot/efi")
    rootPath = InputBox("Enter root path to search", "Path Input", "/boot/efi")
    If rootPath = "" Then Exit Sub
    
    ' Convert path to space-delimited format (e.g. "boot efi")
    searchPath = Replace(Trim(rootPath), "/", " ")
    searchPath = Trim(searchPath)
    pathParts = Split(searchPath, " ")
    targetLevel = UBound(pathParts) + 1
    
    Set dict = CreateObject("Scripting.Dictionary")
    
    ' Get data range
    Set ws = ThisWorkbook.Sheets("Sheet1")
    lastRow = ws.Cells(Rows.count, 1).End(xlUp).Row
    
    
    ' Process data rows
        For i = 1 To lastRow
        Dim currentPath As String
        Dim currentParts() As String
        
        currentPath = ws.Cells(i, 1).Value
        For k = 2 To 10
            If ws.Cells(i, k).Value = "" Then
             Exit For
            End If
            currentPath = currentPath & " " & ws.Cells(i, k).Value
        Next k
        
        currentPath = Replace(Trim(currentPath), "  ", " ")
        currentParts = Split(currentPath, " ")
        
        ' Process only when hierarchy level matches search path +1
        If UBound(currentParts) = targetLevel Then
            Dim matchFlag As Boolean
            Dim j As Long
            
            matchFlag = True
            For j = 0 To targetLevel - 1
                If currentParts(j) <> pathParts(j) Then
                    matchFlag = False
                    Exit For
                End If
            Next j
            
            If matchFlag Then
                ' Add item avoiding duplicates
                'If Not dict.exists(currentParts(targetLevel)) Then
                If Not ws.Cells(i, targetLevel + 1) = "" Then
                    dict.Add currentParts(targetLevel), ""
                End If
                
                
                
            End If
        End If
    Next i
    
    ' Convert results to string
    resultStr = Join(dict.keys, vbCrLf)
    
    ' スYケ訷ヲ
    If dict.count > 0 Then
        MsgBox resultStr, vbInformation, "Search Results"
    Else
        MsgBox "No matching items found", vbExclamation
    End If
End Sub



```vb
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?