File Exchange

image thumbnail

FileTime

version 1.6.0.0 (16.5 KB) by Jan
Get and set the times for creation, access and modification of a file or folder (Windows only)

15 Downloads

Updated 29 Jun 2011

View Version History

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

Cite As

Jan (2021). FileTime (https://www.mathworks.com/matlabcentral/fileexchange/24671-filetime), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (28)

Jan

@Zihan: I do not see a number after the seconds. Do you mean the fractional seconds?

Zihan

Zihan

Hi,Jan.The function is very useful.However,I have a question:In the output time, what does the number after the second mean?

Jan

@V R: Exactly. This submission is working under Windows only, as mentioned in the help section and the description above.

V R

Compiling C code in macOS gives an error

Gonzalo Lerner

Works perfect (Windows 10 64bits, Matlab R2018a)! Thank you!

Jan

@John Harris: The pre-compiled files are available now.

John Harris

As JL1202 mentioned, I can't find the precompiled mex files for this submission at http://www.n-simon.de/mex

Jan

@Lingfei Tang: Sorry! As mentioned in the description, this submission works under Windows only:

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

Lingfei Tang

Is this for Windows only? No wonder the compile did not work in Mac haha

Jan

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

JL1202

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

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

Patrick

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

Works as description!

Josh Philipson

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

Rusty Shackleford

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

harun artuner

very usefull dll for me.
thanks alot.

David Hildebrand

Artik Crazy

Lasse

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

Jan

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

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

Jan

@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

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.

Jan

@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

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.

Aurelien Queffurust

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

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!