import ballerina/file;
import ballerina/log;

// In this example, the listener monitors any modifications done to a specific directoy.
// Before running the example, change the value of the 'path' field 
// to indicate the path of the directory that you want the listener 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.
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 resource is invoked once a new file is created in the listening directory.
    resource function onCreate(file:FileEvent m) {
        string msg = "Create: " + m.name;
        log:printInfo(msg);
    }

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

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

Directory Listener

The Directory Listener is used to listen to a 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.

import ballerina/file;
import ballerina/log;
listener file:Listener inFolder = new ({
    path: "/home/ballerina/fs-server-connector/observed-dir",
    recursive: false
});

In this example, the listener monitors any modifications done to a specific directoy. Before running the example, change the value of the ‘path’ field to indicate the path of the directory that you want the listener 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.

service localObserver on inFolder {

The directory listener should have at least one of these predefined resources.

    resource function onCreate(file:FileEvent m) {
        string msg = "Create: " + m.name;
        log:printInfo(msg);
    }

This resource is invoked once a new file is created in the listening directory.

    resource function onDelete(file:FileEvent m) {
        string msg = "Delete: " + m.name;
        log:printInfo(msg);
    }

This resource is invoked once an existing file is deleted from the listening directory.

    resource function onModify(file:FileEvent m) {
        string msg = "Modify: " + m.name;
        log:printInfo(msg);
    }
}

This resource is invoked once an existing file is modified in the listening directory.

# Before running the sample, create a new file called `test1.txt` in the directory called `observed-dir` and modify and delete it.
# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run directory_listener.bal
Create: /home/ballerina/fs-server-connector/observed-dir/test1.txt
Modify: /home/ballerina/fs-server-connector/observed-dir/test1.txt
Delete: /home/ballerina/fs-server-connector/observed-dir/test1.txt