| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Excel VBA ディレクトリの再帰処理

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Excel VBA ディレクトリの再帰処理

Excel VBA |

FileSystemObjec

http://msdn.microsoft.com/ja-jp/library/cc392182.aspx

ディレクトリの再帰処理をDir関数を使って書こうと思ったら、困難そうなので、FileSystemObjectを使う。

Const FileAttrNormal = 0
Const FileAttrReadOnly = 1
Const FileAttrHidden = 2
Const FileAttrSystem = 4
Const FileAttrVolume = 8
Const FileAttrDirectory = 16
Const FileAttrArchive = 32
Const FileAttrAlias = 64
Const FileAttrCompressed = 128

Private fso As Object

Public Sub recursiveProc()
    Dim d       As String
    Dim folder  As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set folder = fso.GetFolder("c:\")
    
    Call traverse(folder)

End Sub

Private Function traverse(path As Object) As Object
    Dim sfs As Object
    Dim sf  As Object
    Dim fs  As Object
    Dim f   As Object
    
    If path.Attributes And FileAttrDirectory Then
        Debug.Print "DIR : " & path
        Set sfs = path.subFolders
        For Each sf In sfs
            Call traverse(sf)
        Next
            
        Set fs = path.Files
        For Each f In fs
            Call traverse(f)
        Next
    End If
    
    If path.Attributes And FileAttrArchive Then
        Debug.Print "FILE : " & path
    End If

End Function