Dir Function

Purpose

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.

Syntax

Dir[$][(fname$ [,attr])

fname : svar
attr : ivar

Description

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:

AttributeDescription
0All files, except hidden, system, and directories
1Same as 0
2All files, including hidden files (and hidden directories if 16 is specified)
4All files, including system files (and system directories if 16 is specified)
8Only Volume label (if any other attribute is specified this is ignored)
16All files, including directories

Dir without parameters gets the next file. When the last file is reached Dir returns an empty string.

Example

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

Remarks

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:

  • A the archive bit is set (identification for PKZIP, ARJ, RAR, BACKUP etc.)
  • - no directory (not D)
  • - no hidden file (not H)
  • - no system file (not S)
  • - not Read-Only (write protected) (not R)
  • 1000 the length of the file is 1000 bytes
  • 30.10.1995 date of the file
  • 00:00:00 mid night
  • TEST.DAT name of he file - MS-DOS convention (8.3)
  • Test.Dat name of the file - Windows 32 bit file name (small/large, long......)
  • // 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.

    See Also

    DirPush, DirPop, DirPopAll, LongFileName() , ShortFileName(), FileDateTime$(), GetAttr(), FileLen(),ChDir, CurDir(), Dir To

    {Created by Sjouke Hamstra; Last updated: 08/09/2023 by James Gaite}