id: brl.filesystem title: BRL.FileSystem
The BlitzMax filesystem module contains commands to perform operations on the computer's files and directories.
OpenFile, ReadFile and WriteFile return a stream object for reading and or writing data to files.
Directories can be examined file by file using a combination of the ReadDir, NextFile and CloseDir commands, or LoadDir can be used to read the file names of a directory into a string array.
File properties can be examined with the FileType, FileTime, FileSize and FileMode commands.
Files and directories (folders) can be created and deleted with the CreateFile, CreateDir DeleteFile and DeleteDir commands.
Finally, the FileSystem module contains various utility functions for handling file paths in a system independent manner. These commands include RealPath, StripDir, StripExt, StripAll, ExtractDir and ExtractExt.
Function StripDir$( path$ )
Strip directory from a file path
' stripdir.bmx
print stripdir("mypath/myfile.bmx") 'prints myfile.bmx
Function StripExt$( path$ )
Strip extension from a file path
' stripext.bmx
print stripext("mypath/myfile.bmx") 'prints mypath/myfile
Function StripAll$( path$ )
Strip directory and extension from a file path
' stripall.bmx
print stripall("mypath/myfile.bmx") 'prints myfile
Function StripSlash$( path$ )
Strip trailing slash from a file path
StripSlash will not remove the trailing slash from a 'root' path. For example, "/" or (on Win32 only) "C:/".
' stripslash.bmx
print stripslash("mypath/") 'prints mypath
Function ExtractDir$( path$ )
Extract directory from a file path
' extractdir.bmx
print extractdir("mypath/myfile.bmx") 'prints mypath
Function ExtractExt$( path$ )
Extract extension from a file path
' extractext.bmx
print extractext("mypath/myfile.bmx") 'prints bmx
Function CurrentDir$()
Get Current Directory
The current directory
' currentdir.bmx
cd$=currentdir()
print "CurrentDir()="+cd$
Function RealPath$( path$ )
Get real, absolute path of a file path
' realpath.bmx
print realpath("realpath.bmx") 'prints full path of this source
print realpath("..") 'prints full path of parent directory
Function FileType:Int( path$ )
Get file type
0 if file at path doesn't exist, FILETYPE_FILE (1) if the file is a plain file or FILETYPE_DIR (2) if the file is a directory
' filetype.bmx
print filetype(".") 'prints 2 for directory type
print filetype("filetype.bmx") 'prints 1 for file type
print filetype("notfound.file") 'prints 0 for doesn't exist
Function FileTime:Int( path$, timetype:Int=FILETIME_MODIFIED )
Get file time
The time the file at path was last modified
' filetime.bmx
print filetime("filetime.bmx")
Function FileSize:Long( path$ )
Get file size
Size, in bytes, of the file at path, or -1 if the file does not exist
' filesize.bmx
' the following prints the size of this source file in bytes
print filesize("filesize.bmx")
Function FileMode:Int( path$ )
Get file mode
file mode flags
' filemode.bmx
' the following function converts the file mode to
' the standard unix permission bits string
Function Permissions$(mode)
local testbit,pos
local p$="rwxrwxrwx"
testbit=%100000000
pos=1
while (testbit)
if mode & testbit res$:+mid$(p$,pos,1) else res$:+"-"
testbit=testbit shr 1
pos:+1
wend
return res
End Function
print Permissions$(filemode("filemode.bmx"))
Function SetFileMode( path$,Mode:Int )
Set file mode
' setfilemode.bmx
' the following makes this source file readonly
writebits=%010010010
' read the file mode
mode=filemode("setfilemode.bmx")
'mask out the write bits to make readonly
mode=mode & writebits
'set the new file mode
setfilemode("setfilemode.bmx",mode)
Function CreateFile:Int( path$ )
Create a file
True if successful
' createfile.bmx
success=createfile("myfile")
if not success runtimeerror "error creating file"
Function CreateDir:Int( path$,recurse:Int=False )
Create a directory
If recurse is true, any required subdirectories are also created.
True if successful
' createdir.bmx
success=createdir("myfolder")
if not success runtimeerror "error creating directory"
Function DeleteFile:Int( path$ )
Delete a file
True if successful
' deletefile.bmx
success=deletefile("myfile")
if not success runtimeerror "error deleting file"
Function RenameFile:Int( oldpath$,newpath$ )
Renames a file
True if successful
Function CopyFile:Int( src$,dst$ )
Copy a file
True if successful
Function CopyDir:Int( src$,dst$ )
Copy a directory
True if successful
Function DeleteDir:Int( path$,recurse:Int=False )
Delete a directory
Set recurse to true to delete all subdirectories and files recursively - but be careful!
True if successful
' deletedir.bmx
success=deletedir("myfolder")
if not success runtimeerror "error deleting directory"
Function ChangeDir:Int( path$ )
Change current directory
True if successful
' changedir.bmx
print "CurrentDir()="+currentdir()
' change current folder to the parent folder
changedir ".."
' print new CurrentDir()
print "CurrentDir()="+currentdir()
Function ReadDir:Byte Ptr( path$ )
Open a directory
A directory handle, or 0 if the directory does not exist
' readdir.bmx
dir=ReadDir(CurrentDir())
If Not dir RuntimeError "failed to read current directory"
Repeat
t$=NextFile( dir )
If t="" Exit
If t="." Or t=".." Continue
Print t
Forever
CloseDir dir
Function NextFile$( dir:Byte Ptr )
Return next file in a directory
File name of next file in directory opened using ReadDir, or an empty string if there are no more files to read.
Function CloseDir( dir:Byte Ptr )
Close a directory
Function LoadDir$[]( dir$,skip_dots:Int=True )
Load a directory
The skip_dots parameter, if true, removes the '.' (current) and '..' (parent) directories from the returned array.
A string array containing contents of dir
' loaddir.bmx
' declare a string array
local files$[]
files=loaddir(currentdir())
for t$=eachin files
print t
next
Function OpenFile:TStream( url:Object,readable:Int=True,writeable:Int=True )
Open a file for input and/or output.
This command is similar to the OpenStream command but will attempt to cache the contents of the file to ensure serial streams such as http: based url's are seekable. Use the CloseStream command when finished reading and or writing to a Stream returned by OpenFile.
' openfile.bmx
' the following prints the contents of this source file
file=openfile("openfile.bmx")
if not file runtimeerror "could not open file openfile.bmx"
while not eof(file)
print readline(file)
wend
closestream file
Function ReadFile:TStream( url:Object )
Open a file For Input.
This command is similar to the ReadStream command but will attempt to cache the contents of the file to ensure serial streams such as http: based url's are seekable. Use the CloseStream command when finished reading and or writing to a Stream returned by OpenFile.
' readfile.bmx
' the following prints the contents of this source file
file=readfile("readfile.bmx")
if not file runtimeerror "could not open file openfile.bmx"
while not eof(file)
print readline(file)
wend
closestream file
Function WriteFile:TStream( url:Object )
Open a file for output.
This command is identical to the WriteStream command.
' writefile.bmx
file=writefile("test.txt")
if not file runtimeerror "failed to open test.txt file"
writeline file,"hello world"
closestream file
Function CloseFile( stream:TStream )
Closes a file stream.
After performing file operations on an open file make sure to close the file stream with either CloseFile or the identical CloseStream command.