MATLAB Answers

How to get creation date of files

168 views (last 30 days)
Gen on 8 Jun 2016
Commented: Austin Spencer on 10 Jun 2019
How can I get the creation date of file? I can get modified date with dir command but not the creation date.


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 8 Jun 2016
Edited: Stephen Cobeldick on 8 Jun 2016
Try this FEX submission:
Or on windows you can call the DOS command:
but their advice is not quite correct, it actually needs to be like this:
>> [~,str] = dos('dir /T:C *.m');
>> rgx = '(\d{4}\.\d{2}\.\d{2}\.\s+\d{2}:\d{2})\s+\d+\s+([^\n]+)';
>> tkn = regexp(str,rgx,'tokens');
>> tkn{:}
ans =
'2016.03.02. 11:51' 'startup.m'
ans =
'2016.06.07. 15:29' 'Untitled.m'
And then it reads the creation date perfectly!


Gen on 8 Jun 2016
Thank you very much Stephen. It seems I need Windows. Do you know anything work with Mac like this??
Stephen Cobeldick
Stephen Cobeldick on 8 Jun 2016
POSIX (which MAC OS X is compliant with) does not require the storage of the file creation date. Linux does not store this. But apparently OSX does keep this information somehow.... use your favorite internet search engine and you will find the required commands. Then you just need to find a way to call them from MATLAB (probably via system). Good luck!
Gen on 4 Feb 2019
Thank you very much.
I'll try these program.

Sign in to comment.

More Answers (2)

Guillaume on 8 Jun 2016
On windows you can simply delegate to .Net
d = system.IO.File.GetCreationTime(fullpath)

  1 Comment

Austin Spencer
Austin Spencer on 10 Jun 2019
Thanks for the info, this is a nice clean solution! By comparison parsing the return from "dir" seems to be very fragile and is inherently platform dependent.
One correction: at least for my case, "system" must be capitalized.
d = System.IO.File.GetCreationTime(fullpath);
The resulting .NET DateTime object can then be converted into a MATLAB datetime object.
creationDateTime = datetime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);

Sign in to comment.

Tim Lueth
Tim Lueth on 29 Jan 2019
Edited: Walter Roberson on 29 Jan 2019
On Mac OSX (tested 2019-01-29 using Mojave and Matlab R2018a) use:
[a,b]=system('GetFileInfo myfile.m'); s=strfind(b,'created: ')+9; crdat=b(s:s+18)
[a,b]=system(sprintf('GetFileInfo "%s"',fname)); s=strfind(b,'created: ')+9; crdat=b(s:s+18)


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by