指定フォルダ内の指定ファイル


  • ソース
' GetFileList
' 指定フォルダ内の指定ファイルのリストを作成する
' 引数:あり
' iFolderPath   : フォルダのフルパス
' iFilePattern  : 検索ファイルのパターン
' 戻り値:Collection:条件に一致するファイルのリスト
Public Function GetFileList(ByVal iFolderPath As String, ByVal iFilePattern As String) As Collection
    Dim resultCollection As Collection
    Dim fileName As String
    
    Set resultCollection = New Collection
    fileName = Dir(iFolderPath & iFilePattern, vbNormal)
    
    ' ファイルが存在する限りリストに格納
    Do While fileName <> ""
        resultCollection.Add fileName
        fileName = Dir()
    Loop
    
    ' リストの返却
    Set GetFileList = resultCollection
End Function


  • 呼び出しサンプル

Public Sub test_GetFileList()
   Dim colObj As Collection
   Set colObj = GetFileList("C:\TEST\", "*.txt")
   MsgBox colObj.Item(1)
End Sub


最終更新:2013年05月08日 23:55