Blitz3D+ Command Reference

NextFile$ ( dir )

Parameters

dir - directory handle returned by ReadDir

Description

Returns the next entry from a directory opened with ReadDir.

Each call hands you one entry name - a file or a subfolder - and an empty string ("") once there is nothing left, which is your loop's exit condition.

Two things to remember. First, the listing includes the special entries "." and ".." (the directory itself and its parent) - skip those. Second, you get just the name, no path: to inspect an entry with FileType or open it, prefix the folder you are reading, e.g. FileType(folder$+"\"+name$).

You can only move forwards through the listing. To start again, CloseDir and ReadDir the folder afresh.

See also: ReadDir, CloseDir, FileType.

Example

; NextFile 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

; Open the folder for reading - the handle feeds NextFile$
dir=ReadDir("demo_files")

Print "Listing of demo_files:"

; NextFile$ returns the next entry each call, moving forward only.
; "" means the folder is exhausted (the . and .. entries may appear too)
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