Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vala get file modification date

Tags:

vala

I'm new to Vala and linux programming in general.

Im am trying to enumerate the data similar to the 'stat' shell utility for a given folder. So far it's this i got:

int main (string[] args) {
    try {
        File directory = File.new_for_path (".");

        if (args.length > 1) {
            directory = File.new_for_commandline_arg (args[1]);
        }

        FileEnumerator enumerator = directory.enumerate_children (FileAttribute.TIME_MODIFIED, 0);

        FileInfo file_info;


        while ((file_info = enumerator.next_file ()) != null) {

          DateTime t = file_info.get_modification_date_time();

        }

    } catch (Error e) {
        stderr.printf ("Error: %s\n", e.message);
        return 1;
    }

    return 0;
}

Console output:

vala --pkg gio-2.0 --pkg glib-2.0 main3.vala 
main3.vala:16.24-16.59: error: The name `get_modification_date_time' does not exist in the context of `GLib.FileInfo?'

Could someone point me in the right direction? Thanks.

like image 938
theroflcopter Avatar asked Jun 09 '26 17:06

theroflcopter


1 Answers

The error is saying the method doesn't exist. Looking at Valadoc.org for get_modification_date_time it shows this was introduced in GLib version 2.62. That version was released 05 September 2019. It is likely your distribution doesn't include that release yet.

You can either try to update your version of GLib or use the now deprecated get_modification_time:

int main(string[] args) {
    if (args[1] == null) {
        stderr.printf("No filename given\n");
        return 1;
    }
    var file = GLib.File.new_for_path (args[1]);

    try {
        GLib.FileInfo info = file.query_info("*", FileQueryInfoFlags.NONE);
        print (info.get_modification_time().to_iso8601() + "\n");

        print ("\n\nFull info:\n");
        foreach (var item in info.list_attributes (null)) {
            print( @"$item - $(info.get_attribute_as_string (item))\n" );
        }
    } catch (Error error) {
        stderr.printf (@"$(error.message)\n");
        return 1;
    }
    return 0;
}
like image 53
AlThomas Avatar answered Jun 11 '26 18:06

AlThomas