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

MyMemoWiki

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

提供: MyMemoWiki
2020年2月16日 (日) 04:25時点におけるPiroto (トーク | 投稿記録)による版
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

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

Excel VBA |

FileSystemObjec

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

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

  1. Const FileAttrNormal = 0
  2. Const FileAttrReadOnly = 1
  3. Const FileAttrHidden = 2
  4. Const FileAttrSystem = 4
  5. Const FileAttrVolume = 8
  6. Const FileAttrDirectory = 16
  7. Const FileAttrArchive = 32
  8. Const FileAttrAlias = 64
  9. Const FileAttrCompressed = 128
  10.  
  11. Private fso As Object
  12.  
  13. Public Sub recursiveProc()
  14. Dim d As String
  15. Dim folder As Object
  16. Set fso = CreateObject("Scripting.FileSystemObject")
  17. Set folder = fso.GetFolder("c:\")
  18. Call traverse(folder)
  19.  
  20. End Sub
  21.  
  22. Private Function traverse(path As Object) As Object
  23. Dim sfs As Object
  24. Dim sf As Object
  25. Dim fs As Object
  26. Dim f As Object
  27. If path.Attributes And FileAttrDirectory Then
  28. Debug.Print "DIR : " & path
  29. Set sfs = path.subFolders
  30. For Each sf In sfs
  31. Call traverse(sf)
  32. Next
  33. Set fs = path.Files
  34. For Each f In fs
  35. Call traverse(f)
  36. Next
  37. End If
  38. If path.Attributes And FileAttrArchive Then
  39. Debug.Print "FILE : " & path
  40. End If
  41.  
  42. End Function