Filtering Dropbox /delta results by path

Posted by Steve Marx on December 12, 2013

The /delta endpoint is useful for Core API apps that need to track changes inside Dropbox. As I wrote in my last post, it can also be used to simply enumerate all the files in Dropbox. With the recent addition of the path_prefix parameter, it can also now be used to enumerate and track changes to a single path.

Today we added support in the Java, Ruby, and PHP SDKs for the path_prefix parameter. Supporting in other languages is coming soon. Not much needs to change in your code to start using the path_prefix parameter. In PHP, getDelta now takes an optional path_prefix parameter. Similarly, for Ruby, delta gets an extra optional parameter too. In Java, a new method was introduced called getDeltaWithPathPrefix.

An example in Java

The following code shows how to use getDeltaWithPathPrefix in Java to monitor changes to a single path in Dropbox. To run this code yourself, you'll need an access token for your account, which you can either get from your existing code or by following the Core API Java tutorial.

import com.dropbox.core.*;
import java.util.Locale;
import java.lang.Thread;

public class PathPrefixDemo {
    public static void main(String[] args) throws DbxException, InterruptedException {
        if (args.length != 2) {
            System.out.println("Usage: java PathPrefixDemo <access token> <path>");
            System.exit(1);
        }

        String accessToken = args[0];
        String path = args[1];

        DbxRequestConfig config = new DbxRequestConfig(
            "PathPrefixBlogPost/1.0", Locale.getDefault().toString());

        DbxClient client = new DbxClient(config, accessToken);

        String cursor = null;

        while (true) {
            DbxDelta<DbxEntry> result = client.getDeltaWithPathPrefix(cursor, path);
            cursor = result.cursor;
            if (result.reset) {
                System.out.println("Reset!");
            }
            for (DbxDelta.Entry entry : result.entries) {
                if (entry.metadata == null) {
                    System.out.println("Deleted: " + entry.lcPath);
                } else {
                    System.out.println("Added or modified: " + entry.lcPath);
                }
            }

            if (!result.hasMore) {
                // Avoid a tight loop by sleeping when there are no more changes.
                // TODO: Use /longpoll_delta instead!
                // See https://www.dropbox.com/developers/blog/63
                Thread.sleep(1000);
            }
        }
    }
}

As always, if you have any questions or feedback, let us know on the developer forum!