clear_directory

Definition: bool res = clear_directory(string path, bool recurse=true)
Description: Clears a directory of all files, but retains the directory structure.

name: the name of the directory whose files you want to remove.
recurse: whether or not to remove files in sub-directories. If false then subdirectories are left completely unaltered.

Returns: false (0) on failure, true (1) on success.
Comments: This never removes any directories, it only ever removes files. It is used to clear a directory structure of all existing files, leaving the structure intact.

Unlike some other routines here, any path separators should be appropriate for the operating system, namely backslash ('\\') on Windows and forwardslash ('/') on Linux.

A trailing slash is optional, it makes no difference whether one is present or not.

The use of a named parameter when setting recurse is recommended, to make the intent clear and the code easier to read.
Example:
if clear_directory("build")=false then
    if file_exists("build") then
        return fatal("Filesystem problem - could not begin a clean build")
    end if
end if
See Also: remove_directory, delete_file