Saturday, December 17, 2011

Watching A Directory For Changes

This is a very simple VALA script that watches a directory for changes (not including sub-directories) then prints out the changed files.

Usage: changewatch /path/to/folder/to/watch


/**
* Watches a directory for changes:
*
* compile: valac changeWatch.vala --pkg gio-2.0
* Copyright 2011-12-09 Joseph Lewis
* Apache License
*/

using GLib;

void on_change (File f) {
print(f.get_path() + "\n");
}

void main (string[] argv) {

if(argv[1] == null)
{
print("Usage: "+argv[0]+" /path/to/file/to/watch\n");
return;
}

GLib.File fp = File.new_for_path(argv[1]);

GLib.FileMonitor mon1;

try {
mon1 = fp.monitor_directory(
GLib.FileMonitorFlags.NONE
);
mon1.changed.connect(on_change);
print("Monitoring: "+fp.get_path()+"\n");
} catch (GLib.Error e) {
print("Error: "+e.message+"\n");
}
GLib.MainLoop loop = new GLib.MainLoop();
loop.run();
}

No comments:

Post a Comment