Dir, Dir$ (functions)

Syntax

Dir$[(filespec$ [,attributes])]

Description

Returns a String containing the first or next file matching filespec$.

If filespec$ is specified, then the first file matching that filespec$ is returned. If filespec$ is not specified, then the next file matching the initial filespec$ is returned.

Comments

Dir$ returns a String, whereas Dir returns a String variant.

The Dir$/Dir functions take the following parameters:

 

Parameter

Description

 

filespec$

String containing a file specification.

If this parameter is specified, then Dir$ returns the first file matching this file specification. If this parameter is omitted, then the next file matching the initial file specification is returned.

If no path is specified in filespec$, then the current directory is used.

 

attributes

Integer specifying attributes of files you want included in the list, as described below. If omitted, then only the normal, read-only, and archive files are returned.

 

An error is generated if Dir$ is called without first calling it with a valid filespec$.

If there is no matching filespec$, then a zero-length string is returned.

 

Wildcards

The filespec$ argument can include wildcards, such as * and ?. The * character matches any sequence of zero or more characters, whereas the ? character matches any single character. Multiple *'s and ?'s can appear within the expression to form complete searching patterns. The following table shows some examples:

 

This pattern

Matches these files

Doesn't match these files

 

*S*.TXT

SAMPLE.TXT
GOOSE.TXT
SAMS.TXT

SAMPLE
SAMPLE.DAT

 

C*T.TXT

CAT.TXT

CAP.TXT
ACATS.TXT

 

C*T

CAT
CAP.TXT

CAT.DOC

 

C?T

CAT
CUT

CAT.TXT
CAPIT
CT

 

*

(All files)

 

 

Attributes

You can control which files are included in the search by specifying the optional attributes parameter. The Dir, Dir$ functions always return all normal, read-only, and archive files (ebNormal Or ebReadOnly Or ebArchive). To include additional files, you can specify any combination of the following attributes (combined with the Or operator):

 

Constant

Value

Includes

 

ebNormal

0

Normal, Read-only, and archive files

 

ebHidden

2

Hidden files

 

ebSystem

4

System files

 

ebVolume

8

Volume label

 

ebDirectory

16

DOS subdirectories

Example

This example uses Dir to fill a SelectBox with the first 10 directory entries.

Const crlf = Chr$(13) + Chr$(10)
Option Base 1

Sub Main()

  Dim a$(10)
  i% = 1
  a(i%) = Dir("*.*")

 

 While (a(i%) <> "") and (i% < 10)
    i% = i% + 1
    a(i%) = Dir
  Wend

 

  r = SelectBox("Top 10 Directory Entries",,a)
End Sub

See Also

ChDir (statement); ChDrive (statement); CurDir, CurDir$ (functions); MkDir (statement); RmDir (statement); FileList (statement).

More information

D