/ lang / dir
Dir
Syntax

    File name array = 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 matches 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, by specifying one or a combination of the file type constants. If Filter is not specified, all files and directories are returned.

| The file names returned are relative, they do not contain 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 users home directory

      DIM fileName AS String
      FOR EACH fileName IN Dir(User.Home, "[^.]*")
        PRINT fileName
      NEXT

Print png and jpeg images in the users home directory

      DIM directory AS String
      DIM files AS String[]
      DIM fileName AS String
      directory = User.Home
      files = Dir(directory, "*.png")
      files.Insert(Dir(directory, "*.jpg"))
      files.Insert(Dir(directory, "*.jpeg"))
      FOR EACH fileName IN files
        PRINT fileName
      NEXT

Print files only in the users home directory

      DIM fileName AS String
      FOR EACH fileName IN Dir(User.Home, "*", gb.File)
        PRINT fileName
      NEXT

Print sub directories only in the users home directory

      DIM directoryName AS String
      FOR EACH directoryName IN Dir(User.Home, "*", gb.Directory)
        PRINT directoryName
      NEXT

Print non hidden sub directories in the users home directory

      DIM directoryName AS String
      FOR EACH directoryName IN Dir(User.Home, "[^.]*", gb.Directory)
        PRINT directoryName
      NEXT

List system devices

      DIM deviceName AS String
      FOR EACH deviceName IN Dir("/dev", "*", gb.Device)
        PRINT deviceName
      NEXT

### See also

File & Directory Functions