Dir Function

個々のファイルやディレクトリの名前および、指定した検索パスに該当するドライブやディレクトリの中にある、すべてのファイルおよびディレクトリの名前を返します。

構文:

Dir [(Text As String) [, Attrib As Integer]]

戻り値:

文字列

パラメーター:

Text: 検索パス、ディレクトリ、ファイルを指定する文字列表式。この引数を指定できるのは、最初に Dir 関数を呼び出す場合だけです。必要であれば、パス指定にURL 指定を用いることもできます。

Attrib:ファイル属性の指定ビットを示す整数表式。Dir 関数は、指定した属性に該当するファイルやディレクトリドライブのみを返します。こうした属性指定は、該当する値を加算することで、複数の指定を組み合わせることが可能です。

0 : 通常のファイル。

16 : ディレクトリの名前のみを返す。

この属性指定は、特定のファイルやディレクトリが存在するかをチェックしたい場合や、1 つのディレクトリ中にあるすべてのファイルとフォルダー名を確認する場合などに使用します。

特定のファイルが存在するかをチェックするには、該当ファイルの絶対パスとファイル名を指定します。該当するファイルやディレクトリが存在しなかった場合、Dir 関数は文字長ゼロの空白文字列 ("") を返します。

To generate a list of all existing files in a specific directory, proceed as follows: The first time you call the Dir function, specify the complete search path for the files, for example, "D:\Files\*.ods". If the path is correct and the search finds at least one file, the Dir function returns the name of the first file that matches the search path. To return additional file names that match the path, call Dir again, but with no arguments.

To return directories only, use the attribute parameter. The same applies if you want to determine the name of a volume (for example, a hard drive partition).

Error codes:

5 無効なプロシージャー呼び出しです

53 ファイルが見つかりません

例:

Sub ExampleDir

REM すべてのファイルとディレクトリの表示

Dim sPath As String

Dim sDir As String, sValue As String

    sDir="Directories:"

    sPath = CurDir

    sValue = Dir$(sPath + getPathSeparator + "*",16)

    Do

        If sValue <> "." And sValue <> ".." Then

            If (GetAttr( sPath + getPathSeparator + sValue) And 16) >0 Then

                REM ディレクトリの取得

                sDir = sDir & chr(13) & sValue

            End If

        End If

        sValue = Dir$

    Loop Until sValue = ""

    MsgBox sDir,0,sPath

End Sub