.. _LokFileSystemUtil: ================= LokFileSystemUtil ================= **Inherits:** The :ref:`LokFileSystemUtil ` class provides utilities to deal with the file system. Description =========== The objective of this class is to help with boilerplate code when manipulating files or directories in the file system. **Version**: 1.0.0 **Author**: Daniel Sousa (github.com/nadjiel _) Method Index ============ .. list-table:: :header-rows: 1 * - Return type - Signature * - :ref:`void ` - :ref:`push_error_directory_creation_failed `\(:ref:`String ` path, :ref:`int ` error_code\)* - :ref:`void ` - :ref:`push_error_file_writing_or_creation_failed `\(:ref:`String ` path, :ref:`int ` error_code\)* - :ref:`void ` - :ref:`push_error_directory_not_found `\(:ref:`String ` path\)* - :ref:`void ` - :ref:`push_error_file_not_found `\(:ref:`String ` path\)* - :ref:`void ` - :ref:`push_error_file_reading_failed `\(:ref:`String ` path, :ref:`int ` error_code\)* - :ref:`void ` - :ref:`push_error_json_parse_failed `\(:ref:`JSON ` json, :ref:`int ` error_code\)* - :ref:`void ` - :ref:`push_error_directory_or_file_removal_failed `\(:ref:`String ` path, :ref:`int ` error_code\)* - :ref:`int ` - :ref:`create_directory `\(:ref:`String ` path\)* - :ref:`int ` - :ref:`create_directory_if_not_exists `\(:ref:`String ` path\)* - :ref:`bool ` - :ref:`directory_exists `\(:ref:`String ` path\)* - :ref:`PackedStringArray ` - :ref:`get_file_names `\(:ref:`String ` path, :ref:`String `[] formats = ``[]``\)* - :ref:`PackedStringArray ` - :ref:`get_subdirectory_names `\(:ref:`String ` path\)* - :ref:`bool ` - :ref:`directory_is_empty `\(:ref:`String ` path\)* - :ref:`int ` - :ref:`remove_directory_or_file `\(:ref:`String ` path\)* - :ref:`int ` - :ref:`remove_directory_recursive `\(:ref:`String ` path\)* - :ref:`int ` - :ref:`remove_directory_if_exists `\(:ref:`String ` path\)* - :ref:`int ` - :ref:`remove_directory_recursive_if_exists `\(:ref:`String ` path\)* - :ref:`String ` - :ref:`get_directory_name `\(:ref:`String ` directory_path\)* - :ref:`int ` - :ref:`write_or_create_file `\(:ref:`String ` path, :ref:`String ` content = ``""``\)* - :ref:`int ` - :ref:`write_or_create_encrypted_file `\(:ref:`String ` path, :ref:`String ` encryption_pass, :ref:`String ` content = ``""``\)* - :ref:`int ` - :ref:`create_file_if_not_exists `\(:ref:`String ` path\)* - :ref:`int ` - :ref:`create_encrypted_file_if_not_exists `\(:ref:`String ` path, :ref:`String ` encryption_pass, :ref:`String ` content = ``""``\)* - :ref:`bool ` - :ref:`file_exists `\(:ref:`String ` path\)* - :ref:`String ` - :ref:`read_file `\(:ref:`String ` path\)* - :ref:`String ` - :ref:`read_encrypted_file `\(:ref:`String ` path, :ref:`String ` encryption_pass, :ref:`bool ` suppress_errors = ``false``\)* - :ref:`Variant ` - :ref:`parse_json_from_string `\(:ref:`String ` string, :ref:`bool ` suppress_errors\)* - :ref:`Resource `[] - :ref:`load_resources `\(:ref:`String ` directory_path, :ref:`String ` resource_type = ``""``\)* - :ref:`int ` - :ref:`remove_file_if_exists `\(:ref:`String ` path\)* - :ref:`String ` - :ref:`join_file_name `\(:ref:`String ` file_prefix, :ref:`String ` file_format\)* - :ref:`String ` - :ref:`get_file_name `\(:ref:`String ` file_path\)* - :ref:`String ` - :ref:`get_file_format `\(:ref:`String ` file_name\)* - :ref:`String ` - :ref:`get_file_prefix `\(:ref:`String ` file_name\) Method Descriptions =================== .. _LokFileSystemUtil_push_error_directory_creation_failed: :ref:`void ` push_error_directory_creation_failed\(:ref:`String ` path, :ref:`int ` error_code\) ------------------------------------------------------------------------------------------------------------------- The :ref:`push_error_directory_creation_failed ` method is used to push an error when a directory creation fails. The ``path`` of where the creation was tried is expected in the parameters together with an ``error_code`` indicating what kind of error happened. .. _LokFileSystemUtil_push_error_file_writing_or_creation_failed: :ref:`void ` push_error_file_writing_or_creation_failed\(:ref:`String ` path, :ref:`int ` error_code\) ------------------------------------------------------------------------------------------------------------------------- The :ref:`push_error_file_writing_or_creation_failed ` method is used to push an error when a file creation or writing operation fails. The ``path`` of where the operation was tried is expected in the parameters together with an ``error_code`` indicating what kind of error happened. .. _LokFileSystemUtil_push_error_directory_not_found: :ref:`void ` push_error_directory_not_found\(:ref:`String ` path\) -------------------------------------------------------------------------------- The :ref:`push_error_directory_not_found ` method is used to push an error warning that the ``path`` doesn't point to an existing directory. .. _LokFileSystemUtil_push_error_file_not_found: :ref:`void ` push_error_file_not_found\(:ref:`String ` path\) --------------------------------------------------------------------------- The :ref:`push_error_file_not_found ` method is used to push an error warning that the ``path`` doesn't point to an existing file. .. _LokFileSystemUtil_push_error_file_reading_failed: :ref:`void ` push_error_file_reading_failed\(:ref:`String ` path, :ref:`int ` error_code\) ------------------------------------------------------------------------------------------------------------- The :ref:`push_error_file_reading_failed ` method is used to push an error when a file reading fails. The ``path`` of where the operation was tried is expected in the parameters together with an ``error_code`` indicating what kind of error happened. .. _LokFileSystemUtil_push_error_json_parse_failed: :ref:`void ` push_error_json_parse_failed\(:ref:`JSON ` json, :ref:`int ` error_code\) ------------------------------------------------------------------------------------------------------- The :ref:`push_error_json_parse_failed ` method is used to push an error when a :ref:`JSON ` parsing fails. The ``json`` argument is the :ref:`JSON ` instance that failed and the ``error_code`` argument represents the :ref:`@GlobalScope.Error <@GlobalScope_Error>` that occured. .. _LokFileSystemUtil_push_error_directory_or_file_removal_failed: :ref:`void ` push_error_directory_or_file_removal_failed\(:ref:`String ` path, :ref:`int ` error_code\) -------------------------------------------------------------------------------------------------------------------------- The :ref:`push_error_directory_or_file_removal_failed ` method is used to push an error when a directory or file removal fails. The ``path`` where the removal was tried is expected in the parameters together with an ``error_code`` indicating what kind of error happened. .. _LokFileSystemUtil_create_directory: :ref:`int ` create_directory\(:ref:`String ` path\) ---------------------------------------------------------------- The :ref:`create_directory ` method creates a new directory in the path specified by the ``path`` parameter. If an error occurs, this method pushes it, either way, this method returns an :ref:`@GlobalScope.Error <@GlobalScope_Error>` code specifying the success of the operation. .. _LokFileSystemUtil_create_directory_if_not_exists: :ref:`int ` create_directory_if_not_exists\(:ref:`String ` path\) ------------------------------------------------------------------------------ The :ref:`create_directory_if_not_exists ` method uses the :ref:`directory_exists ` and :ref:`create_directory ` methods to create a directory only if it doesn't already exist. If it already exists, this method returns the ``ERR_ALREADY_EXISTS`` error. .. _LokFileSystemUtil_directory_exists: :ref:`bool ` directory_exists\(:ref:`String ` path\) ------------------------------------------------------------------ The :ref:`directory_exists ` method checks if a directory exists in the path specified by the ``path`` parameter and returns a ``bool`` indicating the result. .. _LokFileSystemUtil_get_file_names: :ref:`PackedStringArray ` get_file_names\(:ref:`String ` path, :ref:`String `[] formats = ``[]``\) ------------------------------------------------------------------------------------------------------------------------------------- The :ref:`get_file_names ` method scans the files of a directory in a given ``path`` and returns their names in a :ref:`PackedStringArray `. The ``formats`` parameter is used to filter what file formats should be included in the final result (without the "."). If this parameter is left as default, that means all file formats are included. If the ``path`` doesn't point to an existing directory, an error is pushed and the method returns an empty :ref:`PackedStringArray `. .. _LokFileSystemUtil_get_subdirectory_names: :ref:`PackedStringArray ` get_subdirectory_names\(:ref:`String ` path\) -------------------------------------------------------------------------------------------------- The :ref:`get_subdirectory_names ` method returns the names of the subdirectories of a directory in a given ``path`` in a :ref:`PackedStringArray `. If the ``path`` doesn't point to an existing directory, an error is pushed and the method returns an empty :ref:`PackedStringArray `. .. _LokFileSystemUtil_directory_is_empty: :ref:`bool ` directory_is_empty\(:ref:`String ` path\) -------------------------------------------------------------------- The :ref:`directory_is_empty ` method looks for the files and subdirectories in the directory in the ``path`` and tells whether that directory is empty or not. If the ``path`` doesn't point to an existing directory, an error is pushed and the method returns ``true``, indicating that the directory is empty because it doesn't exist. .. _LokFileSystemUtil_remove_directory_or_file: :ref:`int ` remove_directory_or_file\(:ref:`String ` path\) ------------------------------------------------------------------------ The :ref:`remove_directory_or_file ` method removes a directory or file from the path specified by the ``path`` parameter. If ``path`` points to a directory that isn't empty this method won't succed, so make sure to empty it first. If an error occurs, this method pushes it, either way, this method returns an :ref:`@GlobalScope.Error <@GlobalScope_Error>` code specifying the success of the operation. .. _LokFileSystemUtil_remove_directory_recursive: :ref:`int ` remove_directory_recursive\(:ref:`String ` path\) -------------------------------------------------------------------------- The :ref:`remove_directory_recursive ` method removes a directory from the path specified by the ``path`` parameter, making sure to remove all the subdirectories or files within it. If an error occurs during the operation, this method pushes it and cancels, either way, this method returns an :ref:`@GlobalScope.Error <@GlobalScope_Error>` code specifying the success of the operation. .. _LokFileSystemUtil_remove_directory_if_exists: :ref:`int ` remove_directory_if_exists\(:ref:`String ` path\) -------------------------------------------------------------------------- The :ref:`remove_directory_if_exists ` method uses the :ref:`directory_exists ` and :ref:`remove_directory_or_file ` methods to remove a directory only if exists. If it doesn't exist, this method returns the ``ERR_DOES_NOT_EXIST`` error. .. _LokFileSystemUtil_remove_directory_recursive_if_exists: :ref:`int ` remove_directory_recursive_if_exists\(:ref:`String ` path\) ------------------------------------------------------------------------------------ The :ref:`remove_directory_recursive_if_exists ` method uses the :ref:`directory_exists ` and :ref:`remove_directory_or_file ` methods to remove a directory only if exists. If it doesn't exist, this method returns the ``ERR_DOES_NOT_EXIST`` error. .. _LokFileSystemUtil_get_directory_name: :ref:`String ` get_directory_name\(:ref:`String ` directory_path\) ---------------------------------------------------------------------------------- The :ref:`get_directory_name ` method is a utility method that grabs the name of a directory from a ``directory_path``. .. _LokFileSystemUtil_write_or_create_file: :ref:`int ` write_or_create_file\(:ref:`String ` path, :ref:`String ` content = ``""``\) ------------------------------------------------------------------------------------------------------------- The :ref:`write_or_create_file ` method creates a new file in the path specified by the ``path`` parameter, if it doesn't already exists, else, it simply writes in that file. Optionally, this method can receive a ``content`` parameter that defines what should be written in the file. If an error occurs during the operation, this method pushes it and cancels, either way, this method returns an :ref:`@GlobalScope.Error <@GlobalScope_Error>` code specifying the success of the operation. .. _LokFileSystemUtil_write_or_create_encrypted_file: :ref:`int ` write_or_create_encrypted_file\(:ref:`String ` path, :ref:`String ` encryption_pass, :ref:`String ` content = ``""``\) --------------------------------------------------------------------------------------------------------------------------------------------------------------- The :ref:`write_or_create_encrypted_file ` method creates a new file in the path specified by the ``path`` parameter, if it doesn't already exist, else, it simply writes in that file using encryption. The ``encryption_pass`` parameter is used as the password to encrypt the contents of the file. Optionally, this method can receive a ``content`` parameter that defines what should be written in the file. If an error occurs during the operation, this method pushes it and cancels, either way, this method returns an :ref:`@GlobalScope.Error <@GlobalScope_Error>` code specifying the success of the operation. .. _LokFileSystemUtil_create_file_if_not_exists: :ref:`int ` create_file_if_not_exists\(:ref:`String ` path\) ------------------------------------------------------------------------- The :ref:`create_file_if_not_exists ` method uses the :ref:`file_exists ` and :ref:`write_or_create_file ` methods to create a file only if it doesn't already exist. If it already exists, this method returns the ``ERR_ALREADY_EXISTS`` error. .. _LokFileSystemUtil_create_encrypted_file_if_not_exists: :ref:`int ` create_encrypted_file_if_not_exists\(:ref:`String ` path, :ref:`String ` encryption_pass, :ref:`String ` content = ``""``\) -------------------------------------------------------------------------------------------------------------------------------------------------------------------- The :ref:`create_encrypted_file_if_not_exists ` method uses the :ref:`file_exists ` and :ref:`write_or_create_encrypted_file ` methods to create an encrypted file only if it doesn't already exist. If it already exists, this method returns the ``ERR_ALREADY_EXISTS`` error. .. _LokFileSystemUtil_file_exists: :ref:`bool ` file_exists\(:ref:`String ` path\) ------------------------------------------------------------- The :ref:`file_exists ` method checks if a file exists in the path specified by the ``path`` parameter and returns a ``bool`` indicating the result. .. _LokFileSystemUtil_read_file: :ref:`String ` read_file\(:ref:`String ` path\) --------------------------------------------------------------- The :ref:`read_file ` method reads from a file in the path specified by the ``path`` parameter. If an error occurs, this method pushes it and returns ``""``, otherwise, it returns the :ref:`String ` read from the file. .. _LokFileSystemUtil_read_encrypted_file: :ref:`String ` read_encrypted_file\(:ref:`String ` path, :ref:`String ` encryption_pass, :ref:`bool ` suppress_errors = ``false``\) ----------------------------------------------------------------------------------------------------------------------------------------------------------------- The :ref:`read_encrypted_file ` method reads from a encrypted file in the path specified by the ``path`` parameter. The ``encryption_pass`` parameter is used as the password to decrypt the contents of the file. If an error occurs, this method pushes it and returns ``""``, otherwise, it returns the :ref:`String ` read from the file. If the ``suppress_errors`` is ``true``, though, no errors are pushed, except for errors that come from the :ref:`FileAccess.open_encrypted_with_pass ` method. .. _LokFileSystemUtil_parse_json_from_string: :ref:`Variant ` parse_json_from_string\(:ref:`String ` string, :ref:`bool ` suppress_errors\) -------------------------------------------------------------------------------------------------------------------- The :ref:`parse_json_from_string ` method can be used to parse a :ref:`String ` into a :ref:`Dictionary ` using a :ref:`JSON ` instance. If the parsing fails, an error is pushed and an empty :ref:`Dictionary ` is returned. If the ``suppress_errors`` is ``true``, though, no errors are pushed. .. _LokFileSystemUtil_load_resources: :ref:`Resource `[] load_resources\(:ref:`String ` directory_path, :ref:`String ` resource_type = ``""``\) ----------------------------------------------------------------------------------------------------------------------------------- The :ref:`load_resources ` method is a quick way to load all :ref:`Resource `s located in a specific ``directory_path``. Optionally, a ``resource_type`` :ref:`String ` can be passed to filter what types of :ref:`Resource `s should be loaded or to prevent unknown file types from being loaded. As an example, if a ``"Script"`` :ref:`String ` is passed in the ``resource_type`` parameter, only files with formats that can represent :ref:`Script