Returns a String representing the name of a file, directory or folder that matches a specified file attribute(s), or the volume label of a drive.
Dir[$][(fname$ [,attr])
fname : svar
attr : ivar
The fname specifies a file name - this may include a directory (folder) and drive letter. A zero-length string ("") is returned if fname is not found. Dir supports the use of multiple character (*) and single character (?) wildcards to specify multiple files.
The attr parameter specifies the file attribute(s) of the files to include. If omitted, the value of zero is assumed.
The following values can be used in the attr parameter:
Attribute | Description |
---|---|
0 | All files, except hidden, system, and directories |
1 | Same as 0 |
2 | All files, including hidden files (and hidden directories if 16 is specified) |
4 | All files, including system files (and system directories if 16 is specified) |
8 | Only Volume label (if any other attribute is specified this is ignored) |
16 | All files, including directories |
Dir without parameters gets the next file. When the last file is reached Dir returns an empty string.
To display the first file in a directory:
OpenW 1
Local a%
PrintScroll = True
Print Dir("c:\Windows\*.dll")
Display all files in a directory (comparable to the MSDOS dir /a/b command):
OpenW 1
Local contents$, a%
PrintScroll = True
contents$ = Dir("c:\windows\*", $16)
While Len(contents$)
Print contents$
contents$ = Dir$
Wend
It is possible to combine the attributes with a binary Or. In this way Dir$("*", 16 | 6) lists all normal and hidden files, and names of (hidden) directories, including "." and ".."“.
// Directory - example
Global file$, a$, b$, Attr As Int
file$ = Dir$("*", $16)
While Len(file$) : a$ = "" : Attr = GetAttr()
a$ = a$ + (Attr And 32 ? "A" : "-")
a$ = a$ + (Attr And 16 ? "D" : "-")
a$ = a$ + (Attr And 4 ? "S" : "-")
a$ = a$ + (Attr And 2 ? "H" : "-")
a$ = a$ + (Attr And 1 ? "R" : "-")
If Attr And 16 Then
a$ = a$ + " <Dir>"
Else
a$ = a$ + Str$(FileLen(), 8)
EndIf
a$ = a$ + " "
a$ = a$ + Date$(FileDateTime()) + _
" " + Time$(FileDateTime()) + " "
// extension: time of the last access(date)
//a$ = a$ + Date$(FileDateTimeAccess() + " "
b$ = ShortFileName()
If b$ = "" : b$ = file$ : EndIf
a$ = a$ + Str$(b$, 14) + " "
// Str$(string, cnt) returns a string which will filled with
// spaces, same like: Right$(string, cnt, 32)
// a$=a$+str$(ShortFileName(), 14) + " "
// ShortFileName() returns an empty string if file$ will
// fit to the MS-DOS name
a$ = a$ + file$
Print a$
file$ = Dir
Wend
This program creates the same output as the MS-DOS command DIR /a, similar to that shown below:
A---- 1000 30.10.1995 00:00:00 TEST.DAT Test.Dat
Description:
// The same program now for VB,
// it works both in GFA-BASIC 32 and VB
Global file$, a$, b$, Attr As Int
file$ = Dir$("*", &H16)
While Len(file$)
a$ = "" : Attr = GetAttr(file$)
a$ = a$ + Iif(Attr And 32, "A", "-")
a$ = a$ + Iif(Attr And 16, "D", "-")
a$ = a$ + Iif(Attr And 4, "S", "-")
a$ = a$ + Iif(Attr And 2, "H", "-")
a$ = a$ + Iif(Attr And 1, "R", "-")
If Attr And 16 Then
a$ = a$ + " <Dir>"
Else
b$ = Str$(FileLen(file$))
b$ = Space$(8 - Len(b$)) + b$
'a$ = a$ + Format(FileLen(file$), _
' "* #######0")
a$ = a$ + b$
EndIf
a$ = a$ + " "
If file$ <> "." And file$ <> ".." Then _
a$ = a$ + Format(FileDateTime(file$), _
"dd.mm.yyyy hh:nn:ss ")
'b$ = ShortFileName()
'If b$ = "" : b$ = file$ : EndIf
'a$ = a$ + Str$(b$, 14) + " "
a$ = a$ + file$
Print a$
file$ = Dir
Wend
To list the contents of the subdirectories as well use DirPush and DirPop.
DirPush, DirPop, DirPopAll, LongFileName() , ShortFileName(), FileDateTime$(), GetAttr(), FileLen(),ChDir, CurDir(), Dir To
{Created by Sjouke Hamstra; Last updated: 08/09/2023 by James Gaite}