MATLAB Answers

Is it possible to obtain the creation date of a directory or file in MATLAB 7.5 (R2007b)?

152 views (last 30 days)
I am trying to obtain the creation date of a directory or a file. I am able to obtain the modification date of a file by using the DIR command. For example:
d = dir('myfile.txt');
moddate = d.date;
I want to know if there is a similar command that returns the creation date.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Jun 2017
The ability to directly obtain the file or directory creation date is not available in MATLAB.
As a workaround, use the operating system interface to obtain the creation date of a file or directory. The DOS command allows one to execute functions from the Windows system shell. DIR then gives the file information stored by the operating system. For example, if your working directory contains the file 'myfile.txt' you may execute the following to obtain the creation date of 'myfile.txt':
[dum,str] = dos('dir myfile.txt');
It is then necessary to parse the string, 'str', which is returned from the operating system. This can be done using the TEXTSCAN command. For example:
c = textscan(str,'%s');
createdate = c{1}{15}
Note that proper indexing into the variable 'c' may be platform dependent.
In order for the same MATLAB code to work on a Linux platform, the function ISPC can be used to determine whether the code is running on a PC or Linux platform. On Linux, the commands UNIX and LS work analogously to DOS and DIR.

  0 Comments

Sign in to comment.

More Answers (1)

Jeffrey
Jeffrey on 27 Feb 2018
When I used the method in the previous answer, my results only contained the " MODIFIED " date, not the " CREATED " date.
As it turns out, the previous answer can be altered slightly to obtain the creation date. The difference is in the syntax for the dos command.
To obtain the creation date information use:
dir /T:C filename
To obtain the modification date information use:
dir /T:W filename
EXAMPLE:
Extending this methodology to include the file CREATION date/time and file MODIFIED date/time into a structure "FILEINFO" can be done using function below.
function fileInfo = getfileinfo(filename)
%DOS STRING FOR "FILE CREATION" DATE
dosStr = char(strcat('dir /T:C', {' '}, filename));
[~,str] = dos(dosStr);
c = textscan(str,'%s');
fileInfo.CreationDate = c{1}{15};
fileInfo.CreationTime = datestr(...
datenum(strcat(c{1}{16},{' '}, c{1}{17}), 'HH:MM AM'), 'HH:MM');
%DOS STRING FOR "FILE LAST MODIFIED" DATE
dosStr = char(strcat('dir /T:W', {' '}, filename));
[~,str] = dos(dosStr);
c = textscan(str,'%s');
fileInfo.ModifiedDate = c{1}{15};
fileInfo.ModifiedTime = datestr(...
datenum(strcat(c{1}{16},{' '}, c{1}{17}), 'HH:MM AM'), 'HH:MM');
end
I'm running Windows 7 Professional. Have not tested this method using any other Windows editions.

  1 Comment

LS_SL
LS_SL on 13 Sep 2018
This is super! I did a bit of editing to find files, size, and plot the trend (accumulated size vs. time).
function fileInfo = getfileinfo(filename)
% This function search for the numbre of files written or created on
% specific date and plot a bargraph. The filename is a string variable as
% below.
% filename = 'S:\Support Operations\Support Data\SRS MapCHECK';
% Then, type in Matlab command prompt as below
% >> getfileinfo('S:\SO\Support Data\Product1')
% This function is not idiot proof - Erros can occur if used outside of
% scope.
tic
% DOS string for "FILE WRITTEN" data indicating /tw.
% For 'File Creation Date', use /tc
dosStr = char(strcat({'dir /s /tw '}, {'"'}, filename,{'"'}));
% Reading DOS results
[~,results] = dos(dosStr);
c = textscan(results,'%s');
% Extract file size (fileSize) and date created (dateCreated)
i = 1;
for k = 1:length(c{1})
if (isequal(c{1}{k},'PM')||isequal(c{1}{k},'AM'))&&(~isequal(c{1}{k+1},'<DIR>'))
dateCreated(i,1) = datetime(cell2mat(strcat(c{1}{k-2},{' '},c{1}{k-1},{' '},c{1}{k})),'InputFormat','MM/dd/uuuu hh:mm aa');
n = length(str2num(c{1}{k+1}));
fileSize(i,1) = sum(fliplr(str2num(c{1}{k+1})).*(10.^(3.*((0:n-1)))));
i = i + 1;
end
end
% Sort according to date of creation
[dateCreated,id] = sort(dateCreated);
fileSize = fileSize(id);
% plot a bar graph
span = days(dateCreated(end)-dateCreated(1));
t = span/(length(fileSize)-1);
figure('Name',filename,'MenuBar', 'none','ToolBar', 'none')
if ~isequal(span, 0)
bar(0:t:span,cumsum(fileSize)/2^20,'stacked')
xlabel('Duratiion (days)')
else
bar(cumsum(fileSize)/2^20,'stacked')
xlabel('No. of Files over 1 day')
end
ylabel('Cumulative data (MB)')
title(filename)
info = sprintf('Contains: %d files, %d, bytes.\nRate of Data Accum. ~ %0.2G GB/year.\nWritten dates:\n Start: %s\n End: %s',...
length(fileSize), sum(fileSize),(365*sum(fileSize))/(2^30*span),...
dateCreated(1),dateCreated(end));
text(span*0.02,sum(fileSize)*.9/2^20,info);
toc
end

Sign in to comment.

MathWorks Support

Community Treasure Hunt

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

Start Hunting!