Cyclone 3DR Script
from Technodigit, part of Hexagon. Copyright 1997-2025.
Loading...
Searching...
No Matches
SFile Class Reference

Provide an interface for reading from and writing to files on disk. More...

Public Types

enum  EntriesType { Files = 1 , Directories = 2 }
 Type of entries. More...
 
enum  OpenModeEnum {
  ReadOnly = 1 , WriteOnly = 2 , ReadWrite = 3 , Append = 4 ,
  Truncate = 8
}
 The opening modes. More...
 

Public Member Functions

boolean AtEnd ()
 Return if the current read and write position is at the end of the file (i.e. there is no more data available for reading on the file).
 
 Close ()
 Close the file.
 
boolean Exists ()
 Check if the file exists.
 
string FileName ()
 Get the path of the SFile.
 
boolean IsOpen ()
 Return if the file is open or not. A file is open if it can be read from and/or written to.
 
boolean Open (OpenModeEnum openMode)
 Open the existing file (The file is always open in text mode).
 
string ReadAll ()
 Read all available data from the file.
 
string ReadLine ()
 Read a line from the file.
 
boolean Remove ()
 Remove the file specified by the FileName().
 
boolean Rename (string newName)
 Rename the file currently specified by FileName() with the given name.
 
 SFile (string name)
 Construct a new file object to represent the file with the given name.
 
string toString ()
 Get the type of the variable.
 
boolean Write (string toWrite)
 Write the content of a string to the file.
 

Static Public Member Functions

static string GetDirectory (string path)
 Get the parent directory of a given path.
 
static string GetFileExtension (string path)
 Get the file extension of a given file.
 
static string GetFileName (string path, boolean withExtension=true)
 Get the file name of a given file path.
 
static Object ListEntries (string dirPath, EntriesType entriesType, boolean recursive=false, Array< string > extensionFilter=[])
 List all entries in a given directory.
 
static SFile New (string name)
 Construct a new file object to represent the file with the given name.
 
static boolean Remove (string fileName)
 Remove the file specified by the fileName given.
 

Detailed Description

Provide an interface for reading from and writing to files on disk.

var file = new SFile("D:/Path/to/file.txt");
file.Open(SFile.OpenModeEnum.WriteOnly);
file.Write("Hello World!");
file.Close();
SFile(string name)
Construct a new file object to represent the file with the given name.

Member Enumeration Documentation

◆ EntriesType

Type of entries.

Enumerator
Files 

File entries.

Directories 

Directory entries.

◆ OpenModeEnum

The opening modes.

Enumerator
ReadOnly 

The file is opened for reading.

WriteOnly 

The file is opened for writing. Note that this mode implies Truncate.

ReadWrite 

The file is opened for reading and writing.

Append 

The file is opened in append mode so that all data is written to the end of the file.

Truncate 

If possible, the file is truncated before it is opened. All earlier contents of the device are lost.

Constructor & Destructor Documentation

◆ SFile()

SFile::SFile ( string name)

Construct a new file object to represent the file with the given name.

Parameters
name(string) The name of the file.

Member Function Documentation

◆ AtEnd()

boolean SFile::AtEnd ( )

Return if the current read and write position is at the end of the file (i.e. there is no more data available for reading on the file).

Returns
(boolean) True if the current read and write position is at the end of the file.

◆ Exists()

boolean SFile::Exists ( )

Check if the file exists.

Returns
(boolean) True if the file exists.

◆ FileName()

string SFile::FileName ( )

Get the path of the SFile.

This will return the path that was used when constructing the SFile object, or the last path set by Rename().

Returns
(string) The file path.

◆ GetDirectory()

static string SFile::GetDirectory ( string path)
static

Get the parent directory of a given path.

If the given path corresponds to a directory, the parent directory is still returned.
The path is not checked for existence.
Only forward slashes are used in the returned path.

See also
SFile.GetFileName(), SFile.GetFileExtension()
Parameters
path(string) The path to analyse.
Returns
(string) The parent directory of the given path, including the final forward slash.

◆ GetFileExtension()

static string SFile::GetFileExtension ( string path)
static

Get the file extension of a given file.

If the given path corresponds to a directory, empty is returned.
The path is not checked for existence.

See also
SFile.GetDirectory(), SFile.GetFileName()
Parameters
path(string) The path to analyse.
Returns
(string) The file extension without the dot.

◆ GetFileName()

static string SFile::GetFileName ( string path,
boolean withExtension = true )
static

Get the file name of a given file path.

If the given path corresponds to a directory, the directory name is returned.
The path is not checked for existence.

See also
SFile.GetDirectory(), SFile.GetFileExtension()
Parameters
path(string) The path to analyse.
withExtension(boolean) If true, the returned file name includes the extension if any. If false, the extension is removed.
Returns
(string) The file name.

◆ IsOpen()

boolean SFile::IsOpen ( )

Return if the file is open or not. A file is open if it can be read from and/or written to.

Returns
(boolean) True if the file is open.

◆ ListEntries()

static Object SFile::ListEntries ( string dirPath,
EntriesType entriesType,
boolean recursive = false,
Array< string > extensionFilter = [] )
static

List all entries in a given directory.

Entries ordering has no guarantee to be consistent.

var dirPath = "D:/TestDirectory/";
var entries = SFile.ListEntries(dirPath, SFile.EntriesType.Files, false, ["txt"]);
if(entries.ErrorCode != 0)
Print("Could not access directory: " + dirPath);
else
{
Print("All text files in " + dirPath + ":");
for(let entry of entries.Entries)
Print(entry);
}
See also
SFile.EntriesType, SFile.GetFileName(), SFile.GetDirectory(), SFile.GetFileExtension()
Parameters
dirPath(string) The path of the directory to explore.
entriesType(EntriesType) The type of entries to list (files and/or directories).
recursive(boolean) If true, all sub-directories will be explored recursively. Don't use for large directory trees as it may take a long time or too much memory. Prefer implementing your own recursive logic using this function.
extensionFilter(Array<string>) An array of extension filters to apply. If empty, all files are listed. This filter only applies to file entries. You can include the leading dot in the extensions or not.
Return values
ret.ErrorCode(number) Error code
  • 0: Success.
  • 1: Could not access the given directory.
  • 2: Unknown error.
ret.Entries(Array<string>) The list of absolute paths to the entries.

◆ New()

static SFile SFile::New ( string name)
static

Construct a new file object to represent the file with the given name.

Parameters
name(string) The name of the file.
Returns
(SFile) The new SFile.

◆ Open()

boolean SFile::Open ( OpenModeEnum openMode)

Open the existing file (The file is always open in text mode).

Parameters
openMode(OpenModeEnum) The opening mode to use.
  • SFile.ReadOnly: The file is opened for reading.
  • SFile.WriteOnly: The file is opened for writing. Note that this mode implies Truncate.
  • SFile.ReadWrite: The file is opened for reading and writing.
  • SFile.Append: The file is opened in append mode so that all data is written to the end of the file.
  • SFile.Truncate: If possible, the file is truncated before it is opened. All earlier contents of the device are lost.
Returns
(boolean) True if the file has been successfully opened.

◆ ReadAll()

string SFile::ReadAll ( )

Read all available data from the file.

Returns
(string) A string with all read content.

◆ ReadLine()

string SFile::ReadLine ( )

Read a line from the file.

Returns
(string) A string with the read line.

◆ Remove() [1/2]

boolean SFile::Remove ( )

Remove the file specified by the FileName().

Returns
(boolean) True if the file has been successfully removed.

◆ Remove() [2/2]

static boolean SFile::Remove ( string fileName)
static

Remove the file specified by the fileName given.

Parameters
fileName(string) The name of the file to remove.
Returns
(boolean) True if the file has been successfully removed.

◆ Rename()

boolean SFile::Rename ( string newName)

Rename the file currently specified by FileName() with the given name.

Parameters
newName(string) The new name of the file.
Returns
(boolean) True if the file has been successfully renamed.

◆ toString()

string SFile::toString ( )

Get the type of the variable.

Returns
(string) The type name

◆ Write()

boolean SFile::Write ( string toWrite)

Write the content of a string to the file.

Parameters
toWrite(string) The string to write in the file
Returns
(boolean) True if the content has been successfully written.