Back to Examples

Directory listener

The file library provides a Directory Listener, which is used to listen to the given directory in the local file system.

It notifies when new files are created in the directory or when the existing files are deleted or modified.

For more information on the underlying module, see the file module.

Before running this, change the value of the path field to indicate the path of the directory that you want to monitor.

As the recursive property is set to false, the listener does not monitor the child directories of the main directory that it listens to.

import ballerina/file;
import ballerina/log;

// The listener monitors any modifications done to a specific directory.
listener file:Listener inFolder = new ({
    path: "/home/ballerina/fs-server-connector/observed-dir",
    recursive: false
});

// The directory listener should have at least one of these predefined resources.
service "localObserver" on inFolder {

    // This function is invoked once a new file is created in the listening directory.
    remote function onCreate(file:FileEvent m) {
        log:printInfo("Create: " + m.name);
    }

    // This function is invoked once an existing file is deleted from the listening directory.
    remote function onDelete(file:FileEvent m) {
        log:printInfo("Delete: " + m.name);
    }

    // This function is invoked once an existing file is modified in the listening directory.
    remote function onModify(file:FileEvent m) {
        log:printInfo("Modify: " + m.name);
    }
}

To run this sample, use the bal run command. After running the sample,

create a new file called test1.txt in the directory called observed-dir, modify it, and delete it.

$ bal run directory_listener.baltime = 2020-12-12 13:49:08,497 level = INFO  module = "" message = "Create: /home/ballerina/fs-server-connector/observed-dir/test1.txt"time = 2020-12-12 13:49:41,709 level = INFO  module = "" message = "Modify: /home/ballerina/fs-server-connector/observed-dir/test1.txt"time = 2020-12-12 13:50:04,997 level = INFO  module = "" message = "Delete: /home/ballerina/fs-server-connector/observed-dir/test1.txt"
PreviousTemp files and directories
NextRandom numbers