項目

ファイルを検索する(Dir)

概要

Dirでファイルを検索する事ができます。
例ではサブフォルダは検索しません。

Option Compare Database
Option Explicit

Private Sub コマンド0_Click()

    Dim pathname As String
    Dim filepath As String

    '@c\temp\hoge.xlsがあるか検索
    pathname = "c:\temp\hoge.xls"

    filepath = Dir(pathname)

    If filepath = "" Then
        Debug.Print "ないよ"
    Else
        Debug.Print "あるよ"
    End If

    'Ac:\tempのEXCELファイルを検索
    pathname = "c:\temp\*.xls"

    '検索するファイルがあった場合ファイル名が格納される
    filepath = Dir(pathname)

    Do Until filepath = ""

        'ファイル名をイミディエイトウィンドウに出力
        Debug.Print filepath

        '次のフォルダを検索
        filepath = Dir

    Loop

End Sub