Public API Documentation

This document describes the public API methods available for file and directory operations along with sample AL usage.


File Operations

Open and Create

Open(Name: Text; DriveAccessFileMode: Enum "Drive Access File Mode"): Boolean
Opens an existing file or creates a new one based on the specified mode.
Parameters:

  • Name: The file name to open or create.
  • DriveAccessFileMode: The file open mode (e.g., Open, Create).
    Returns: Boolean indicating success or failure.

Open(Name: Text): Boolean
Opens an existing file.
Parameters:

  • Name: The file name to open.
    Returns: Boolean indicating success or failure.

Create(Name: Text): Boolean
Creates a new file.
Parameters:

  • Name: The file name to create.
    Returns: Boolean indicating success or failure.

Parameters:

  • Name: File name.
  • DriveAccessFileMode: The mode (e.g., Open, Create).

Sample Code:

var
    DriveFile: Codeunit "Drive Access File";
begin
    // Open an existing file
    if DriveFile.Open('MyFile.txt') then;

    // Create a new file
    if DriveFile.Create('NewFile.txt') then;
end;

Read Operations

Read(var OutStream: OutStream; NumberOfBytes: Integer; var IsEndOfFile: Boolean): Integer
Reads data from an open file into an output stream.
Parameters:

  • OutStream: The output stream for received data.
  • NumberOfBytes: Number of bytes to read (0 for all).
  • IsEndOfFile: Indicates if the end of file was reached.
    Returns: Number of bytes actually read.

Read(var OutStream: OutStream; NumberOfBytes: Integer): Integer
Reads into an output stream without returning end-of-file info.
Parameters:

  • OutStream: Destination for data.
  • NumberOfBytes: Bytes to read (0=all).
    Returns: Bytes read.

Read(var OutStream: OutStream): Integer
Reads all available data into an output stream.
Parameters:

  • OutStream: Destination stream.
    Returns: Bytes read.

Read(var Data: Text; NumberOfBytes: Integer; var IsEndOfFile: Boolean): Integer
Reads text data.
Parameters:

  • Data: Variable to store the text read.
  • NumberOfBytes: Number of bytes to read (0 for all).
  • IsEndOfFile: Indicates if the end of file was reached.
    Returns: Number of bytes actually read.

Read(var Data: Text; NumberOfBytes: Integer): Integer
Reads text data without returning end-of-file info.
Parameters:

  • Data: Destination string.
  • NumberOfBytes: Bytes to read (0=all).
    Returns: Bytes read.

Read(var Data: Text): Integer
Reads all available text data.
Parameters:

  • Data: Destination text.
    Returns: Bytes read.

Sample Code:

var
    DriveFile: Codeunit "Drive Access File";
    OutStream: OutStream;
    BytesRead: Integer;
    EndOfFile: Boolean;
begin
    // Assuming the file is already open
    BytesRead := DriveFile.Read(OutStream, 1024, EndOfFile);
end;

You can also read text:

var
    DriveFile: Codeunit "Drive Access File";
    Data: Text;
    BytesRead: Integer;
begin
    BytesRead := DriveFile.Read(Data, 1024);
end;

Write Operations

Write(var InStream: InStream; NumberOfBytes: Integer): Boolean
Writes binary data from an input stream.
Parameters:

  • InStream: The input stream containing data.
  • NumberOfBytes: The number of bytes to write.
    Returns: Boolean indicating success or failure.

Write(Text: Text): Boolean
Writes text data.
Parameters:

  • Text: The text to write.
    Returns: Boolean indicating success or failure.

Sample Code:

var
    DriveFile: Codeunit "Drive Access File";
    InStream: InStream;
begin
    // To write text
    if DriveFile.Write('Hello World') then;

    // To write from a stream (assume InStream is set up)
    if DriveFile.Write(InStream, 512) then;
end;

Seek and Close

Seek(Position: Integer): Boolean
Moves the file pointer to a specified position.
Parameters:

  • Position: The zero-based position in the file.
    Returns: Boolean indicating success or failure.

Close(): Boolean
Closes the open file.
Returns: Boolean indicating success or failure.

Sample Code:

var
    DriveFile: Codeunit "Drive Access File";
begin
    if DriveFile.Seek(0) then;
    if DriveFile.Close() then;
end;

Directory Operations

ReadDirectory, CreateDirectory, and DeleteDirectory

ReadDirectory(Path: Text; var Rec: Record "Drive Access File"): Boolean
Reads directory contents into a record.
Parameters:

  • Path: The directory path.
  • Rec: Temporary record to store directory entries.
    Returns: Boolean indicating success or failure.

Sample Code:

var
    DriveFile: Codeunit "Drive Access File";
    DirRec: Record "Drive Access File";
begin
    // Read directory contents
    if DriveFile.ReadDirectory('C:\Temp', DirRec) then;

    // Create a new directory
    if DriveFile.CreateDirectory('C:\Temp\NewFolder') then;

    // Delete the directory recursively
    if DriveFile.DeleteDirectory('C:\Temp\NewFolder', true) then;
end;

CreateDirectory(Path: Text): Boolean
Creates a new directory.
Parameters:

  • Path: The directory path to create.
    Returns: Boolean indicating success or failure.

DeleteDirectory(Path: Text; Recursive: Boolean): Boolean
Deletes a directory (optionally recursively).
Parameters:

  • Path: The directory path to delete.
  • Recursive: Whether to remove contents recursively.
    Returns: Boolean indicating success or failure.

File Management

Delete, Rename, Exists, Upload and Download

DeleteFile(Name: Text): Boolean
Deletes the specified file.
Parameters:

  • Name: The file name to delete.
    Returns: Boolean indicating success or failure.

Sample Code:

var
    DriveFile: Codeunit "Drive Access File";
    TempBlob: Codeunit "Temp Blob";
begin
    // Delete a file
    if DriveFile.DeleteFile('OldFile.txt') then;

    // Rename a file
    if DriveFile.Rename('FileA.txt', 'FileB.txt') then;

    // Check existence
    if DriveFile.Exists('FileB.txt') then;

    // Upload file
    if DriveFile.UploadFile('C:\UploadFolder', 'FileB.txt', TempBlob) then;

    // Download file
    if DriveFile.DownloadFile('C:\DownloadFolder\FileB.txt', TempBlob) then;
end;

Rename(FromName: Text; ToName: Text): Boolean
Renames a file.
Parameters:

  • FromName: The existing file name.
  • ToName: The new file name.
    Returns: Boolean indicating success or failure.

Exists(Name: Text): Boolean
Checks if a file exists.
Parameters:

  • Name: The file to check.
    Returns: Boolean indicating the presence of the file.

UploadFile(Path: Text; Filename: Text; TempBlob: Codeunit "Temp Blob"): Boolean
Uploads a file to the given path.
Parameters:

  • Path: The(upload) folder path.
  • Filename: Name of the file to upload.
  • TempBlob: Codeunit for file data.
    Returns: Boolean indicating success or failure.

DownloadFile(Path: Text; var TempBlob: Codeunit "Temp Blob"): Boolean
Downloads a file from the given path.
Parameters:

  • Path: The file path to download.
  • TempBlob: Codeunit for receiving file data.
    Returns: Boolean indicating success or failure.

Copy and Move

Copy(SourcePath: Text; DestinationPath: Text): Boolean
Copies a file from one location to another.
Parameters:

  • SourcePath: The path of the source file.
  • DestinationPath: The target path.
    Returns: Boolean indicating success or failure.

Move(SourcePath: Text; DestinationPath: Text): Boolean
Moves a file from one location to another.
Parameters:

  • SourcePath: The path of the file to move.
  • DestinationPath: The target path.
    Returns: Boolean indicating success or failure.

Sample Code:

var
    DriveFile: Codeunit "Drive Access File";
begin
    // Example use for copy and move when implemented:
    // if DriveFile.Copy('C:\Source\File.txt', 'C:\Destination\File.txt') then;
    // if DriveFile.Move('C:\Source\File.txt', 'C:\Destination\File.txt') then;
end;

Configuration and Setup

SetDriveAccess

SetDriveAccess(DriveAccessCode: Code[20])
Sets up the current drive access using a drive access code.
Parameters:

  • DriveAccessCode: The code referencing the drive access record.
    Returns: None (procedure updates current config).

SetDriveAccess(DriveAccess: Record "Drive Access")
Sets up the drive access using a full record.
Parameters:

  • DriveAccess: The record containing drive access info.
    Returns: None (procedure updates current config).

Sample Code:

var
    DriveFile: Codeunit "Drive Access File";
begin
    // Using a drive access code
    DriveFile.SetDriveAccess('ACCESS_CODE');

    // Or using a drive access record (assume DriveAccessRec is defined and initialized)
    // DriveFile.SetDriveAccess(DriveAccessRec);
end;