CreateDirTree ( directory$ )

Parameters

directory$ - path of the directory tree to create

Description

Recursively creates a directory tree; an already-existing tree counts as success.

Where CreateDir creates one level and fails if the parent is missing, CreateDirTree creates every missing level of a path in one call - "saves\profiles\slot1" springs into existence parents-first. It is also idempotent: calling it on a tree that already exists succeeds quietly, so the standard save-system pattern is simply "CreateDirTree, then write", with no existence checks first.

That matters because the atomic writers - SaveJsonAtomic, WriteTextAtomic, WriteBankAtomic - deliberately never create missing parent folders. CreateDirTree is the half of the pattern that prepares the ground.

In Extended mode the path is strict UTF-8 backed by Unicode Windows calls, so player-named profile folders in any language work. On failure - for example a file already occupying a needed path component, or a permissions problem - it returns 0 and sets the FilesystemError diagnostics; success clears them and returns true.

Like every write command in this family, CreateDirTree is main-thread-only.

For the full rules, see the Application Data language reference.


See also: WriteTextAtomic, SaveJsonAtomic, FilesystemError, CreateDir, DeleteDir.

Example

; CreateDirTree Example
; ---------------------

; A save system often needs a whole folder chain at once.
; CreateDirTree creates every missing level in one call.
If CreateDirTree("dirtree_example\profiles\slot1")=0 Then RuntimeError FilesystemError()
Print "Created dirtree_example\profiles\slot1"

; Calling it again succeeds - an existing tree is not an error
If CreateDirTree("dirtree_example\profiles\slot1")
    Print "Second call also succeeds - the tree already exists."
EndIf
Print ""

; Put a save file inside the new tree to show it is usable
WriteTextAtomic "dirtree_example\profiles\slot1\save.json","{}"
Print "Wrote save.json inside the new tree ("+FileSize("dirtree_example\profiles\slot1\save.json")+" bytes)"

; Clean up everything this example created
DeleteFile "dirtree_example\profiles\slot1\save.json"
DeleteDir "dirtree_example\profiles\slot1"
DeleteDir "dirtree_example\profiles"
DeleteDir "dirtree_example"
Print "Cleaned up the example directories."

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

End

Index