Dir
Result = Dir ( Directory AS String [ , Pattern AS String , Options AS Integer ] ) AS String[]
Return a string array that contains the names of files located in
Directory that match the
Pattern.
The pattern can contain the same generic characters than the
LIKE operator. If no pattern is specified, any file name is returned.
The
Options argument specifies what kind of files will be returned.
It takes 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
Options 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.
Since 3.20
If you add the
gb.FullPath
constant to the
Options argument, the
Directory path is added to each returned file.
Note that the pattern does not apply to the directory part in that case.
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