How to get creation date of files

조회 수: 210 (최근 30일)
Gen
Gen 2016년 6월 8일
댓글: Stephen23 2023년 1월 26일
How can I get the creation date of file? I can get modified date with dir command but not the creation date.

채택된 답변

Stephen23
Stephen23 2016년 6월 8일
편집: Stephen23 2016년 6월 8일
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!
  댓글 수: 4
Gen
Gen 2019년 2월 4일
Thank you very much.
I'll try these program.
KAE
KAE 2021년 7월 1일
If you have spaces in the directory name you will need to put double quotes on the file name, here in a variable called fileName:
[~, str] = dos(['dir "' fileName '"']);

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Guillaume
Guillaume 2016년 6월 8일
On windows you can simply delegate to .Net
d = system.IO.File.GetCreationTime(fullpath)
  댓글 수: 1
Austin Spencer
Austin Spencer 2019년 6월 10일
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);

댓글을 달려면 로그인하십시오.


Sam Raymond
Sam Raymond 2021년 5월 5일
With python now very much integrated into MATLAB (R2021a), a nice way to get this is the following:
d1 = datetime(py.os.path.getctime('video_path'),'ConvertFrom','epochtime','TicksPerSecond',1,'Format','dd-MMM-yyyy HH:mm:ss.SSS');

Tim Lueth
Tim Lueth 2019년 1월 29일
편집: Tim Lueth 2019년 1월 29일
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)
or
fname='myfile.m'
[a,b]=system(sprintf('GetFileInfo "%s"',fname)); s=strfind(b,'created: ')+9; crdat=b(s:s+18)
datestr(datenum(crdat))

Konstantin
Konstantin 2023년 1월 26일
Why not like this?
file_info = dir(path_to_file)
  댓글 수: 1
Stephen23
Stephen23 2023년 1월 26일
"Why not like this?"
The question asks for the file creation date. DIR() returns the file modification date:
This is explained in the DIR() documentation:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by