File Exchange

image thumbnail


version 1.6 (16.5 KB) by

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

8 Ratings



View License

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:

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

Comments and Ratings (18)

Jan Simon

Jan Simon (view profile)

I will update the list of pre-compiled files soon.


JL1202 (view profile)

Hi Jan,
I can't find the precompiled mex files at the given adress. Are they there? Could anybody provide them please, I'm having trouble installing the MinGW-w64 Compiler.
Thanks, J

Jan Simon

Jan Simon (view profile)

@Patrick: This replies the time in 24h format. Therefore 12:00PM should give 12:00, while 12:00am is 00:00. This format is defined by th operating system and the AM/PM should concern the display only.


This was very useful just one quick question. If I have a file that was taken at 12:00PM the script does not detect the "PM". Which in my case is an issue. Has anyone found a workaround to this?


Qingyu (view profile)

Works as description!


Josh (view profile)

Thanks Jan. People like you make this a special place dude.

Exactly what I needed. Let me emphasize to users that you must have the pre-compiled mex file!

very usefull dll for me.
thanks alot.

Artik Crazy


Lasse (view profile)

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

Jan Simon

Jan Simon (view profile)

@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.

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!


Jan Simon

Jan Simon (view profile)

@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.


Drew (view profile)

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

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

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

Any thoughts? Thank you very much.

Jan Simon

Jan Simon (view profile)

@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!


Bass (view profile)

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
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.

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



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


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


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


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.


Access folders also


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

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video