トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

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



目次



記事一覧

キーワード

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

[Excel VBA][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



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.