Name
DirectoryItems -- iterate over all items in a directory (V7.0)
Synopsis
f = DirectoryItems(d$)
Function
This function can be used together with the generic For statement to traverse all files and sub-directories in a directory. It returns an iterator function which will return two values for each directory item: The first return value will be the name of the file or directory, the second return value will be a table with additional information about the directory item. Once all directory items have been returned, the iterator function will return Nil to break the generic For statement.

See Generic For statement for details.

The table that is returned by DirectoryItems() as the second return value when used in a generic For loop will have the following fields initialized:

Type:
This will be #DOSTYPE_FILE if the entry is a file or #DOSTYPE_DIRECTORY if the entry is a directory.

Size:
This field will only be present if the entry is a file. In that case, this field will receive the size of the file in bytes.

Flags:
This field will receive a combination of protection flags of the file or directory. See Protection flags for details.

Time:
This field will receive a string containing the time the file or directory was last modified. The string will always be in the format dd-mmm-yyyy hh:mm:ss. E.g.: 08-Nov-2004 14:32:13.

LastAccessTime:
This field will receive a string containing the time the file or directory was last accessed. This attribute is not supported on AmigaOS.

CreationTime:
This field will receive a string containing the time the file or directory was created. This attribute is only supported on Win32.

Comment:
This field will contain the comment of a file. This is only supported by the Amiga versions.

Note that you can also manually traverse all files and sub-directories inside a directory by using the OpenDirectory(), NextDirectoryEntry() and CloseDirectory() functions. Using DirectoryItems(), however, is often more convenient.

Inputs
d$
directory to traverse
Results
f
iterator function for generic for loop
Example
Function p_TraverseDir(d$, indent)
   For s$,t In DirectoryItems(d$)
      DebugPrint(RepeatStr(" ", indent) .. s$, t.time)
      If t.type = #DOSTYPE_DIRECTORY
        p_TraverseDir(FullPath(d$, s$), indent + 8)
      EndIf
   Next
EndFunction

p_TraverseDir("images", 0)
The function p_TraverseDir() can be used recursively print all files and sub-directories in the given directory. The example call prints the contents of a directory named "images" that must be stored relative to the script's path.

Show TOC