Re: [Vala] Get CREATION DATE OR TIME of file



Hi all,

One possible solution to this problem that would be Windows only is to
retrieve the file creation date via the Windows API. The following code is
untested as I am still setting up my Winodws VM but you might be able to
try it out in the meantime. You will need to install the Windows SDK if you
haven't already and I assume you are using mingw. A caveat, I haven't used
the Windows API in a long time so you may need to double check the headers
to make sure the function definitions are correct - this is for version
7.0. I'm assuming it's possible, as the parts of the Windows API you would
be linking to are C libraries and this is more or less how you would do it
in C# but like I said - this is an untested thought experiment that might
be a total NO-OP. The only thing I could find on the internets was from the
mailing list quite a few years back -
http://comments.gmane.org/gmane.comp.programming.vala/2867 - but it does
suggest that this is entirely possible.

Anyway, the code...

// All of this is Windows API definitions which could be put into a
separate vapi

// The CreateFile flag for reading
const int64 GENERIC_READ = 0x80000000;

// Contains a 64-bit value representing the number of 100-nanosecond
intervals since January 1, 1601 (UTC).
struct FileTime {
    // The low-order part of the file time.
    int64 dwLowDateTime;
    // The high-order part of the file time.
    int64 dwHighDateTime;
}

struct SystemTime {
   int64 wYear;
   int64 wMonth;
   int64 wDayOfWeek;
   int64 wDay;
   int64 wHour;
   int64 wMinute;
   int64 wSecond;
   int64 wMilliseconds;
}

[CCode(cheader_filename = "WinBase.h ")]
public extern bool GetFileTime(void* file_handle,
    out lpCreationTime,
    out lpLastAccessTime,
    out lpLastWriteTime
);

[CCode(cheader_filename = "WinBase.h ")]
public extern void CreateFile(
    string lpFileName,
    int64 dwDesiredAccess,
    int64 dwShareMode,
    void* lpSecurityAttributes,
    int64 dwCreationDisposition,
    int64 dwFlagsAndAttributes,
    void* hTemplateFile
);

[CCode(cheader_filename = "WinBase.h ")]
public extern bool FileTimeToSystemTime (FileTime filetime, out SystemTime);



public static int main (string[] args) {

   void* testfile = CreateFile("./test.vala", GENERIC_READ,0, null, 0, 0,
null);
   FileTime time;
   bool success = GetFileTime(testfile,out time, null, null);

    if (success) {
       SystemTime systime;
       FileTimeToSystemTime (time, out systime);
       message ("Success: File created on %02d/%02d/%d %02d:%02d",
systime.wMonth, systime.wDay, systime.wYear, systime.wHour,
systime.wMinute);
    } else {
      message ("Something went wrong");
   }
}

This would need to be compiled with the right paths set to the Windows SDK
location and linked against Kernel32.lib.

Like I said, it's untested (yet) and I won't have time until next week to
look at it again, so caveat emptor. If you're wanting to make a nicer
demonstration out of it, you could save the Windows extern functions and
struct definitions to a vapi file - like windows.vapi - to make it look
less complicated (which it is, compared to the C code that would achieve
the same effect).

Let me know if you have any success.

Cheers
Chris


2016-01-08 12:35 GMT-08:00 Edwin De La Cruz <edwinspire gmail com>:

Lametablemente no way? At least not directly using Vala , I understand
what I have read so far.
I keep looking for any option , since it was very important for me to
do in Vala and convince some people that language worth learning ...
I'll keep looking.
Mis proyectos de software libre en:
Github - edwinspire


2016-01-08 9:04 GMT-05:00 Luc Chante <luc chante gmail com>:
Hi,

You can test le file a attached to this mail.

This is what I get (under windows with msys2 and vala 0.30) :

$ ./test
** Message: test.vala:21: FileAttribute.TIME_CREATED =>
FileAttributeType.INVALID
** Message: test.vala:56: Can be read !!!
** Message: test.vala:60: FileAttribute.TIME_CREATED =>
FileAttributeStatus.UNSET

Even in C when you are looking for the creation date there is no simple
answer.


http://unix.stackexchange.com/questions/91197/how-to-find-creation-date-of-file#91200

Le jeu. 7 janv. 2016 à 15:27, Edwin De La Cruz <edwinspire gmail com> a
écrit :

Best regards . Attempt to get the date or time unix date of creation
of a file and do not get it , I used the following code :


        var directory = File.new_for_path (path);

var fi = directory.query_info ("*", GLib.FileQueryInfoFlags.NONE);

 stdout.printf ("Date %s \n", fi.get_attribute_as_string
(GLib.FileAttribute.TIME_CREATED));
 stdout.printf ("Date or time %s \n", fi.get_attribute_int64
(GLib.FileAttribute.TIME_MODIFIED).to_string());

I always returns 0 , maybe I'm not doing it correctly , be obliged his
help.

I'm using Windows valac - 0.26 .


Mis proyectos de software libre en:
Github - edwinspire
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list




-- 
Chris Daley
Pacific Northwest

e: chebizarro gmail com
w: http://chrisdaley.biz
m: +1601 980 1249
s: chebizarro
tw: chebizarro
tz: PDT


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]