Working with Files

Files in Simba are all integers, internal handles for Simba which on their turn point to operating system files. Functions like CreateFile and OpenFile return a file handle. You should not forget to close these when you no longer need them.

File System Functions

CreateFile

function CreateFile(const Path: string): Integer;

Create a file with Path. Raturns -1 on failure, otherwise returns the handle to the file.

OpenFile

function OpenFile(const Path: string; Shared: Boolean): Integer;

Opens file for reading. Opens shared if Shared is true. Returns -1 on failure, otherwise returns the handle to the file.

RewriteFile

function RewriteFile(const Path: string; Shared: Boolean): Integer;

Opens file for rewriting. (File is cleared on open) Opens shared if Shared is true. Returns -1 on failure, otherwise returns the handle to the file.

AppendFile

function AppendFile(const Path: string): Integer;

Opens file for writing (appending). Returns -1 on failure, otherwise returns the handle to the file.

CloseFile

procedure CloseFile(FileNum: Integer);

Close the file defined by FileNum. Never forget to close your files!

DeleteFile

function DeleteFile(const Filename: string): Boolean;

Delete the file with name Filename. Returns true on success.

EndOfFile

function EndOfFile(FileNum: Integer): Boolean;

Returns true if the end of the file has been reached.

FileSize

function FileSize(FileNum: Integer): LongInt;

Returns the file size in characters.

ReadFileString

function ReadFileString(FileNum: Integer; var s: string; x: Integer):
Boolean;

Read x characters into string s from file FileNum. Returns true if the number of characters read equals x.

WriteFileString

function WriteFileString(FileNum: Integer; s: string): Boolean;

Writes s to file FileNum. Returns false on failure.

SetFileCharPointer

function SetFileCharPointer(FileNum, cChars, Origin: Integer): Integer;

Seek through the file. Set the cursor to cChars from Origin.

Origin can be any of these:

{ File seek origins }
FsFromBeginning = 0;
FsFromCurrent   = 1;
FsFromEnd       = 2;

FilePointerPos

function FilePointerPos(FileNum: Integer): Integer;

Returns the position of the cursur in the file. (What character # you are at)

DirectoryExists

function DirectoryExists(const DirectoryName : string ) : Boolean;

Returns true if the directory exists.

CreateDirectory

function CreateDirectory(const DirectoryName : string) : boolean;

Creates a directory. Returns true on success.

FileExists

function FileExists (const FileName : string ) : Boolean;

Returns true if the file exists.

ForceDirectories

function ForceDirectories(const dir : string) : boolean;

Creates multiple nested directories. Returns true on success.

GetFiles

function GetFiles(const Path, Ext : string) : TStringArray;

Returns the files in the directory defined by Path with extension Ext. You can also set Ext as ‘*’ to return all files in Path.

GetDirectories

function GetDirectories(const path : string) : TStringArray;

Returns the directories in path.

WriteINI

procedure WriteINI(const Section, KeyName, NewString, FileName: string);

The following example writes to a specific Line in a Specified INI File.

program WriteINIExample;

Var Section, Keyname, NewString, FileName: String;

begin
  Section := 'What subsection in the INI it is being Writen.';
  KeyName := 'Space in the specified Subsection in which you string is writen.';
  NewString := 'What your Writing into the INI file.';
  FileName := 'The Name of the INI File you are writing too.';
  WriteINI(Section, KeyName, NewString, ScriptPath + FileName);
end.

Note

ScriptPath will Automatically point the file finding to the same folder the script is saved in.

Note

This procedure can be used in conjunction with ReadINI to saved Player Data for the next time a script is run.

ReadINI

function ReadINI(const Section, KeyName, FileName: string): string;

The following example writes to a specific Line in a Specified INI File then Reads that line and prints it’s findings.

program WriteINIReference;

Var Section, Keyname, NewString, FileName: String;

begin
  Section := 'What subsection in the INI it is being Writen.';
  KeyName := 'Space in the specified Subsection in which you string is writen.';
  NewString := 'What your Writing into the INI file.';
  FileName := 'The Name of the INI File you are writing too.';
  WriteINI(Section, KeyName, NewString, ScriptPath + FileName);
  Writeln(ReadINI(Section, KeyName, ScriptPath + FileName);
end.

DeleteINI

procedure DeleteINI(const Section, KeyName, FileName: string);

The following example deletes the specific line inside the specified INI file.

program DeleteINIExample;

begin
  DeleteINI('Section', Key, File);
end;

ExtractFileExt

function ExtractFileExt(const FileName: string): string;');

Returns the file extension from file Filename.

DeleteDirectory

function DeleteDirectory(const Dir: String; const Empty: Boolean): Boolean;

Deletes the directory dir, if Empty is true will delete the directorys contents else will not.

RenameFile

function RenameFile(const Oldname, NewName: string): Boolean

UnZipFile

procedure UnZipFile(const FilePath, TargetPath: string)

ZipFiles

procedure ZipFiles(const ToFolder: string; const Files: TstringArray)