Code covered by the BSD License  

Highlights from
FileTime

4.5

4.5 | 2 ratings Rate this file 31 Downloads (last 30 days) File Size: 16.54 KB File ID: #24671

FileTime

by Jan Simon

 

08 Jul 2009 (Updated 29 Jun 2011)

Get and set the times for creation, access and modification of a file or folder (Windows only)

| Watch this File

File Information
Description

These MEX-functions get and set the creation, access and write times of a file under Windows.

SetFileTime(FileName, Spec, DateVector, Type)
  FileName: File or folder name, absolute or relative path.
      Unicode names and UNC paths are accepted.
  Spec: String, 'Creation', 'Access', 'Write'.
  DateVector: [1 x 6] double vector (see DATEVEC) or UINT64.
  Type: Optional, conversion method from local to UTC time.

Time = GetFileTime(FileName, Type, OutType)
  FileName: File or folder name, absolute or relative path.
  Type: Optional, conversion method from UTC to local time.
  OutType: Optional, all three times as struct or a single time.
  Time: Struct with fields 'Creation', 'Access', 'Write' or
        one time, [1x6] DATEVEC or UINT64 format.

The Local<->UTC conversion needs Windows XP, Windows Server 2003 or higher. The LCC 2.4 compiler shipped with Matlab does not know the "new" time function.

Precompiled mex files can be downloaded at:
http://www.n-simon.de/mex

Tested: Matlab 6.5/2009a, WinXP, 32 bit, OWC1.8, BCC5.5, LCC 3.8, MSVC 2008
Compatibility to 64 bit Windows is assumed.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
24 Aug 2009 Aurelien Queffurust

Very useful to get the creation time of files since the DIR output doesn't return this information

05 Nov 2009 Bass

Useful function.
Beaware, however that the times return by this function do not necessarily match those return by the MATLAB dir function. This seems to because Windows add an hour when reporting the time during daylight savings time (even if the file wasn't created during daylight savings time. See http://www.mathworks.com/support/solutions/en/data/1-1YLPX9/index.html?product=ML&solution=1-1YLPX9.)
MATLAB however does not do this.

I this function appears to return the Windows time. So I have run into problems when using the two functions together.

11 Nov 2009 Jan Simon

@Bass: I've submitted a new version with an improved UTC<->local time conversion. Now with the default conversion, GetFileTime replies the same time as the DIR command of Matlab 7. UTC times and the Windows-like times depending on the current daylight saving time are possible also.
Thanks for finding this problem!

14 Oct 2010 Drew

This is the first time I've downloaded from the FEX, so please excuse me if I'm making a trivial mistake; however, I am unable to get this to run.

When I try to use GetFileTime.m I see:

??? Error using ==> GetFileTime at 77
*** GetFileTimeCannot find MEX script!

I then tried running the uTest_FileTime.m and received a similar response:

=== Test SetFileTime / GetFileTime 14-Oct-2010 12:04:21
Create test file:
       name: 'FileTime__.test'
       date: '14-Oct-2010 12:04:21'
      bytes: 0
      isdir: 0
    datenum: 7.3443e+005

GetFileTime:
??? Error using ==> GetFileTime at 77
*** GetFileTimeCannot find MEX script!

Error in ==> uTest_FileTime at 59
Reply = GetFileTime(File);

Any thoughts? Thank you very much.

14 Oct 2010 Jan Simon

@Drew: Sorry, I forgot to mention explicitely, that the C-file has to be compiled before it can be used.
At first setup your compiler by "mex -setup", if not done before. Then compile as mentioned in the C-file: "mex -O GetFileTime.c" and "mex -O SetFileTime.c".
If you run a 32bit Matlab, I offer pre-compiled Mex files on the named webpage.
Unfortunately I'm not allowed to distribute compiled files here in the FEX.

29 Nov 2010 Emile Demarteau

Dear Jan,

Your function looks really promising, however I cannot compile the function, as matlab returns an error on the TzSpecificLocalTimeToSystemTime function. I am running matlab 7.5 on XP so this should support this functon.

Alternatively I commented out the error warning which uses this function. Now compilong works, however the function still return an error: 'SetFileTime: Time to FILETIME failed!' or ''Too many input paramters"

Finally, your precompiled dll's fixed the problem.

So thanks for putting this online!

Cheers,
Emile

29 Nov 2010 Jan Simon

@Emile: I'm happy that offering the binaries helped!
 Which compiler did you use? In the documentation of SetFileTime you find a warning that LCC shipped with Matlab (at least up to 2009a) does not recognize TzSpecificLocalTimeToSystemTime. Very sad, because this function is not really brand new.
I found some workarounds in the net, but the source is much longer and complicated than the SetFileTime source.

11 Dec 2011 Lasse

Thanks so much for this!
Is i possible to make a 'DateTaken'?

Please login to add a comment or rating.
Updates
11 Nov 2009

UTC<->local time conversion more consistent now and flexible. Thanks Bass for pointing to this problem!

20 Nov 2009

Access folders also

20 Aug 2010

Test function failed, if DIR replies localized names for dates as e.g. the German "24-Dez-2010". No problems for English OS versions and in the actual functions.

01 Oct 2010

Accept unicode names.
Another fixed bug in the test function concerning local month names.

01 Oct 2010

Consider Unicode file names.
Another fixed bug concerning local month names in the test function.

29 Jun 2011

Times can have native UINT64 format. GetFileTime can reply a single time, e.g. just 'Access' time.

Tag Activity for this File
Tag Applied By Date/Time
file Jan Simon 09 Jul 2009 10:58:07
time Jan Simon 09 Jul 2009 10:58:08
date Jan Simon 09 Jul 2009 10:58:08
creation Jan Simon 09 Jul 2009 10:58:08
access Jan Simon 09 Jul 2009 10:58:08
write Jan Simon 09 Jul 2009 10:58:08
date Gracie 15 Jul 2009 11:37:58
access fx 23 Jul 2009 11:45:15
ntfs Jan Simon 11 Nov 2009 10:53:48
dir Jan Simon 11 Nov 2009 10:53:49
mex Jan Simon 20 Nov 2009 10:32:27
fat Jan Simon 20 Nov 2009 10:32:27
folder Jan Simon 20 Aug 2010 10:11:33
attributes Jose Ercolino 23 Sep 2011 18:51:32
date Jose Ercolino 23 Sep 2011 18:51:32
time Jose Ercolino 23 Sep 2011 18:51:32
file Jose Ercolino 23 Sep 2011 18:51:32
folder Jose Ercolino 23 Sep 2011 18:51:32
creation Jose Ercolino 23 Sep 2011 18:51:32
modification Jose Ercolino 23 Sep 2011 18:51:32

Contact us at files@mathworks.com