Dir

System function.

The Dir function puts file names from a directory into arrays.

Usage

In an automation task, you often have to repeat the same actions for all the files within a directory, or for all the files with a specific extension. Dir function gives the needed list of files to process. We recommend to use DirTree instead of Dir.

Syntax

Dir(<file_spec$>,tab1$(),tab2$(),tab3$(),"options")
or
ret=Dir(<file_spec$>,tab1$(),tab2$(),tab3$(),"options")

Parameters

<file_spec$>: string such as "c:\mydir\*.txt" or "\\serv\home\dir 1\as??.*", list of files to put in a array. If the path is omitted, the search is performed in the current directory. If option "R" is specified, the files are searched in the current directory and all its subdirectories.

Tab1$(), tab2$(), tab3$() are result arrays. They must have been declared at the beginning of the script with the DIM statement.

Tab1$() returns the long file names of the files found (including hidden or system files), without their paths.

Tab2$() returns the short file names (DOS 8.3 format) of the files found with their paths.

Tab3$() returns the attributes of the files found:

D

directory

R

read only

H

hidden

A

archive

S

system

"options" is a string, modifying the search and sort order of the arrays. It may contain one or more of the following letters:

N

sort by name

S

sort by size

E

sort by type (extension)

D

sort by date

G

put directories first

R

include subdirectories

A

sort by last access date (this is different from last modification date)

Ex : if option = "AN", the list will be sorted by last access date. For identical access dates, files will be sorted by name.

Return value

ret, integer, number of files found. If no items are found, 0 is returned. If an error is detected in an argument (for instance, an invalid path), a return code of -1 is returned; unless #IgnoreErrors=1, all the scripts are stopped.

 

Remarks

If file_spec$ ends with "*.*", the first 2 items in the arrays tab1$() and tab2$() will be "." and ".." as in a DOS DIR command listing. In tab3$, they will appear with the "D" attribute (directory).

If Dir() is used with the R option, file names appear in the specified order, without regard to the directory in which they are found. If the R option is used in a search for "*.*", tab1$() and tab2$() will contain "." and ".." several times (see above).

See also

DirTree

FileAttr$

Example

 

Dim tab1$(100)

Dim tab2$(100)

Dim tab3$(100)

 

spec$="*.lst"

'Returns all the *.LST filenames within the current directory and its sub-directories.
num=dir(spec$,tab1$(),tab2$(),tab3$(),"R")

if num=0 then stop endif

i=0

repeat

msgbox(tab1$(i))

i=i+1

until i>=num