lang • dir

Dir

FilenameArray = Dir ( Directory AS String [ , Pattern AS String , Filter AS Integer ] ) AS String[]

Returns a string array that contains the names of files located in Directory that matched the Pattern and the Filter.

The pattern can contain the same generic characters than the LIKE operator. If no pattern is specified, any file name is returned.

The filter specifies what kind of files will be returned.

The filter can be one of the following values:

  • gb.File for returning only files.

  • gb.Directory for returning only directories.

  • gb.File + gb.Directory for returning both.

If Filter is not specified, all files and directories are returned.

The file names returned are relative to the directory searched. They do not contain the path to the searched directory.

Examples

' Print the png image files in a directory, in alphabetical order.

Sub PrintDirectory(Directory As String)

  Dim File As String

  For Each File In Dir(Directory, "*.png").Sort()
    Print File
  Next

End
' Print all non-hidden files in the user home directory.

Dim fileName As String

For Each fileName In Dir(User.Home, "[^.]*")
  Print fileName
Next
' Print png and jpeg images in the user home directory.

Dim Directory As String
Dim Files As String[]
Dim FileName As String

Directory = System.User.Home
Files = Dir(Directory, "*.png")
Files.Insert(Dir(Directory, "*.jpg"))
Files.Insert(Dir(Directory, "*.jpeg"))

For Each FileName In Files
  Print FileName
Next
' Prints only files in the user home directory.

Dim fileName As String

For Each fileName In Dir(User.Home, "*", gb.File)
  Print fileName
Next
' Prints only sub directories in the user home directory.

Dim directoryName As String

For Each directoryName In Dir(User.Home, "*", gb.Directory)
  Print directoryName
Next
' Prints non-hidden sub directories in the user home directory.

Dim directoryName As String

For Each directoryName In Dir(User.Home, "[^.]*", gb.Directory)
  Print directoryName
Next

See also