ReadDir ( dirname$ )

Parameters

dirname$ - path of the directory to open

Description

Opens a directory so its contents can be listed.

Returns a directory handle, or 0 if the directory could not be opened (wrong path, or it is a file). Feed the handle to NextFile$ to pull out one entry at a time, and CloseDir it when you are done.

This is how a game discovers files it does not know in advance: listing save games for a load menu, finding player-made levels or mods, showing the contents of a screenshots folder. Use FileType on each entry to tell files from subfolders, and remember the listing includes the "." and ".." entries.

See also: NextFile, CloseDir, FileType, CurrentDir.

Example

; ReadDir Example
; ---------------

; Build a small folder tree owned by this example
CreateDir "demo_files"
CreateDir "demo_files\saves"
file=WriteFile("demo_files\readme.txt")
WriteLine file,"Demo file one"
CloseFile file
file=WriteFile("demo_files\config.txt")
WriteLine file,"Demo file two"
CloseFile file

; ReadDir opens a folder for reading and returns a handle
; for NextFile$ to iterate with
dir=ReadDir("demo_files")

Print "Listing of demo_files:"

; NextFile$ returns each entry in turn - "" means no more
Repeat
    entry$=NextFile$(dir)
    If entry$="" Then Exit
    ; FileType tells folders (2) from files (1)
    If FileType("demo_files\"+entry$)=2 Then
        Print "  <DIR>  "+entry$
    Else
        Print "  file   "+entry$
    EndIf
Forever

; Always close the folder when you are done with it
CloseDir dir

; Clean up everything the example created
DeleteFile "demo_files\readme.txt"
DeleteFile "demo_files\config.txt"
DeleteDir "demo_files\saves"
DeleteDir "demo_files"
Print ""
Print "Cleaned up demo_files"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index