FileManager Logo

Overview

The FileManager class provides methods for managing files and directories in the system. It allows you to create, delete, copy, and move files and folders, as well as read and write JSON data.

import src.hxFileManager.FileManager;

Variables

rootDir

Returns the root directory from which the program is located.

Returns: String
var rootDirectory:String = FileManager.rootDir;

isAdmin

Checks if the program is running with administrative privileges.

Check the checkIfAdmin function.

Returns: Bool
var adminAccess:Bool = FileManager.isAdmin;

Methods

createFile(filePath: String, content: String)

Creates a new file at the specified filePath with the provided content.

Parameters:
  • filePath (String): The path where the file will be created.
  • content (String): The content to write into the file.
Returns: Void
fileManager.createFile("path/to/file.txt", "Hello World!");

readFile(filePath: String) (DEPRECATED!!!)

Reads the content of a file at the specified filePath and returns it as a string.

Parameters:
  • filePath (String): The path to the file to read.
Returns: String
var content = fileManager.readFile("path/to/file.txt");

readFileAsync(filePath: String, onSuccess: (String) -> Void, onError: (Dynamic) -> Void)

Reads the content of a file asynchronously and triggers callbacks for success or error.

Parameters:
  • filePath (String): The path to the file to read.
  • onSuccess (Function): Callback function triggered on successful file read.
  • onError (Function): Callback function triggered in case of an error.
Returns: Void

fileManager.readFileAsync("path/to/file.txt", function(content) {
    trace(content);
}, function(error) {
    trace("Error: " + error);
});
                

getFileMetadata(filePath: String)

Gets metadata (such as size and last modified date) of a file at the specified filePath.

Parameters:
  • filePath (String): The path to the file.
Returns: StringMap<Dynamic>
var metadata = fileManager.getFileMetadata("path/to/file.txt");

deleteFile(filePath: String)

Deletes the file at the specified filePath if it exists.

Parameters:
  • filePath (String): The path to the file to delete.
Returns: Void
fileManager.deleteFile("path/to/file.txt");

folderExists(folderPath: String)

Checks if a folder exists at the specified folderPath.

Parameters:
  • folderPath (String): The path to the folder to check.
Returns: Bool
var exists = fileManager.folderExists("path/to/folder");

deleteFolder(folderPath: String)

Deletes the folder at the specified folderPath if it exists.

Parameters:
  • folderPath (String): The path to the folder to delete.
Returns: Void
fileManager.deleteFolder("path/to/folder");

renameFolder(folder: String, newFolder: String)

Renames the specified folder to a new name if it exists.

Parameters:
  • folder (String): The current name of the folder.
  • newFolder (String): The new name for the folder.
Returns: Void
fileManager.renameFolder("oldFolderName", "newFolderName");

listFiles(folderPath: String)

Lists all files in the specified folder.

Parameters:
  • folderPath (String): The path to the folder to list files from.
Returns: Array<String>
var files = fileManager.listFiles("path/to/folder");

fileExists(filePath: String)

Checks if a file exists at the specified filePath.

Parameters:
  • filePath (String): The path to the file to check.
Returns: Bool
var exists = fileManager.fileExists("path/to/file.txt");

moveFolder(sourcePath: String, destPath: String)

Moves the folder from the sourcePath to the destPath.

Parameters:
  • sourcePath (String): The path of the folder to move.
  • destPath (String): The new path for the folder.
Returns: Void
fileManager.moveFolder("path/to/sourceFolder", "path/to/destinationFolder");

copyFile(sourcePath: String, destPath: String)

Copies the file from the sourcePath to the destPath.

Parameters:
  • sourcePath (String): The path of the source file.
  • destPath (String): The path where the file will be copied.
Returns: Void
fileManager.copyFile("path/to/sourceFile.txt", "path/to/destinationFile.txt");

renameFile(oldPath: String, newPath: String)

Renames the specified file from the oldPath to the newPath.

Parameters:
  • oldPath (String): The current path of the file.
  • newPath (String): The new path for the file.
Returns: Void
fileManager.renameFile("path/to/oldFile.txt", "path/to/newFile.txt");

createFolder(folderPath: String)

Creates a new folder at the specified folderPath if it doesn't already exist.

Parameters:
  • folderPath (String): The path where the folder will be created.
Returns: Void
fileManager.createFolder("path/to/newFolder");

deletePath(path: String)

Deletes the specified path, whether it is a file or a folder.

Parameters:
  • path (String): The path of the file or folder to delete.
Returns: Void
fileManager.deletePath("path/to/delete");

getAppDataPath()

Returns the path to the AppData folder based on the operating system.

Parameters:
  • None
Returns: String
var appDataPath = fileManager.getAppDataPath();

logOperation(operation: String, path: String, success: Bool)

Logs the details of an operation performed on a path.

Parameters:
  • operation (String): The operation performed.
  • path (String): The path on which the operation was performed.
  • success (Bool): Indicates whether the operation was successful.
Returns: Void
fileManager.logOperation("createFile", "path/to/file.txt", true);

readJson(filePath: String)

Reads a JSON file and returns its content as a dynamic object.

Parameters:
  • filePath (String): The path to the JSON file to read.
Returns: Dynamic
var jsonData = fileManager.readJson("path/to/file.json");

writeJson(filePath: String, data: Dynamic)

Writes or updates a JSON file with the specified data.

Parameters:
  • filePath (String): The path where the JSON file will be written or updated.
  • data (Dynamic): The dynamic object to write to the JSON file.
Returns: Void
fileManager.writeJson("path/to/file.json", jsonData);

checkIfAdmin()

Checks if the current user has administrative privileges based on the operating system.

Parameters:
  • None
Returns: Bool
var isAdmin:Bool = FileManager.checkIfAdmin();