Monkey Documentation

Module brl.filesystem

The filesystem module allows you to inspect and modify files on the filesystem. More...


Constants:
  • FILETYPE_DIR : Int
  • FILETYPE_FILE : Int
  • FILETYPE_NONE : Int
Functions:
  • CopyDir : Bool ( srcpath:String, dstpath:String, recursive:Bool=true, hidden:Bool=false )
  • CopyFile : Bool ( src:String, dst:String )
  • CreateDir : Bool ( path:String )
  • CreateFile : Bool ( path:String )
  • DeleteDir : Bool ( path:String, recursive:Bool=false )
  • DeleteFile : Bool ( path:String )
  • FileSize : Int ( path:String )
  • FileTime : Int ( path:String )
  • FileType : Int ( path:String )
  • LoadDir : String[] ( path:String, recursive:Bool=false, hidden:Bool=false )
  • RealPath : String ( path:String )

Detailed Discussion

The filesystem module allows you to inspect and modify files on the filesystem.

The filesystem module is currently only available for the android, ios, win8, glfw and stdcpp targets.

Example

#If TARGET<>"android" And TARGET<>"ios" And TARGET<>"winrt" And TARGET<>"glfw"
#Error "Invalid target"
#Endif

Import mojo

Import brl.FileSystem

Class MyApp Extends App

    Method OnCreate()
    
        DeleteDir  "monkey://internal/dir1",True
        DeleteDir  "monkey://internal/dir2",True
        DeleteDir  "monkey://internal/dir3",True
        
        CreateDir  "monkey://internal/dir1"
        CreateFile "monkey://internal/dir1/file1"
        CreateFile "monkey://internal/dir1/file2"
        CreateDir  "monkey://internal/dir1/dir2"
        CreateFile "monkey://internal/dir1/dir2/file5"
        
        CreateDir  "monkey://internal/dir2"
        CreateFile "monkey://internal/dir2/file3"
        CreateFile "monkey://internal/dir2/file4"
        
        CopyDir    "monkey://internal/dir1","monkey://internal/dir3",True
        DeleteFile "monkey://internal/dir1/file1"
        DeleteFile "monkey://internal/dir3/dir2/file5"
        
        SetUpdateRate 60
    End
    
    Method OnUpdate()
    End
    
    Method OnRender()
        Scale 2,2
        Cls
        Local y:=0
        For Local f:=Eachin LoadDir"monkey://internal/",True )
            Local p:="monkey://internal/"+f
            Local nm:=(f+"                    ")[..20]
            Local ty:=""
            If FileTypep )=FILETYPE_FILE
                ty=FileSizep )
            Else
                ty="(dir)"
            Endif
            DrawText nm+ty,0,y
            y+=12
        Next
    End
End

Function Main()

    New MyApp
    
End


Function Documentation

Function CopyDir : Bool ( srcpath:String, dstpath:String, recursive:Bool=true, hidden:Bool=false )

Copies the directory at srcpath to dstpath, creating dstpath if necessary.

If recursive is True, then subdirectories are also copied.

If hidden is True, then hidden files - files starting with a dot - are also copied.

Returns True if the directory was successfully copied.

Function CopyFile : Bool ( src:String, dst:String )

Copies a file from srcpath to dstpath.

Returns True if the file was successfully copied.

Function CreateDir : Bool ( path:String )

Create a new directory at path.

Returns True if the directory was successfully created.

Function CreateFile : Bool ( path:String )

Creates a new file at path.

If the file already exists, it's contents are discarded and a new empty file is created.

Returns True if the file was successfully created.

Function DeleteDir : Bool ( path:String, recursive:Bool=false )

Deletes the directory at path.

If recursive is True, then subdirectories are also deleted - use with care!

If recursive if False and the directory contains files, DeleteDir will fail.

Returns True if the directory was successfully deleted.

Function DeleteFile : Bool ( path:String )

Deletes the specified file.

Returns True if the file was successfully deleted.

Function FileSize : Int ( path:String )

Return the length of a file, in bytes, or 0 if the file does not exist.

Function FileTime : Int ( path:String )

Return the time the file was last modified, or 0 if the file does not exist.

The returned value is the number of seconds since Jan 1 1970.

Function FileType : Int ( path:String )

Returns an integer representing the type of a file, one of: FILETYPE_NONE for no file, FILETYPE_FILE for a normal file, FILETYPE_DIR for a directory.

Function LoadDir : String[] ( path:String, recursive:Bool=false, hidden:Bool=false )

Loads the file names in the directory specified by path into a string array.

If recursive is True, LoadDir will also load file and directory names in subdirectories.

If hidden is True, then hidden files - files starting with a dot - are also loaded.

Function RealPath : String ( path:String )

Converts path to an absolute filesystem path.