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

MyMemoWiki

「Excel VBA ディレクトリの再帰処理」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
 
1行目: 1行目:
==Excel VBA ディレクトリの再帰処理==
+
==[[Excel VBA ディレクトリの再帰処理]]==
[[Excel VBA]] | {{category VBAソース片}}
+
[[Excel VBA]] | [[Category:VBAソース片]]
  
 
===FileSystemObjec===
 
===FileSystemObjec===
8行目: 8行目:
  
 
  Const FileAttrNormal = 0
 
  Const FileAttrNormal = 0
  Const FileAttrReadOnly = 1
+
  Const FileAttr[[R]]eadOnly = 1
 
  Const FileAttrHidden = 2
 
  Const FileAttrHidden = 2
 
  Const FileAttrSystem = 4
 
  Const FileAttrSystem = 4
23行目: 23行目:
 
     Dim folder  As Object
 
     Dim folder  As Object
 
      
 
      
     Set fso = CreateObject("Scripting.FileSystemObject")
+
     Set fso = CreateObject("Scripting.[[FileSystemObject]]")
 
      
 
      
 
     Set folder = fso.GetFolder("c:\")
 
     Set folder = fso.GetFolder("c:\")
38行目: 38行目:
 
      
 
      
 
     If path.Attributes And FileAttrDirectory Then
 
     If path.Attributes And FileAttrDirectory Then
         Debug.Print "DIR : " & path
+
         Debug.Print "DI[[R]] : " & path
 
         Set sfs = path.subFolders
 
         Set sfs = path.subFolders
 
         For Each sf In sfs
 
         For Each sf In sfs

2020年2月16日 (日) 04:25時点における最新版

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