How to obtain the date and time (datenum) of a .txt saved in the harddrive?

I have multiple .txt files with mxn-arrays saved at different date and time.
I wonder if it is possible to write a command line that generates the date and time of the files?
Thank you for your attention
Emerson

 Accepted Answer

D=struct2cell(dir('*.txt'))
file=D(2,:)'
% your files are
fle{1},file{2},
% if your files are in folder "fold" use
dir('fold/*.txt')

4 Comments

Hi Azzi, thank you a lot for your help. Your suggestion almost works, the date is correct but the time not.
For example:
1.txt, 04-Sep-2012, 05:02:09
2.txt, 04-Sep-2012, 05:02:19
3.txt, 04-Sep-2012, 05:02:30
but the file{i} are not in the chronological sequence, so I obtain:
file{1}, 04-Sep-2012, 05:02:09
file{2}, 04-Sep-2012, 05:03:37
file{3}, 04-Sep-2012, 05:18:25
Do you know what is going on and how to fix it?
Thank you again for your help
Emerson
Hi Azzi,
your suggestion is fine, all what I needed to do was to sort ascending to obtain the correct chronological order. I added to your commands sort and datenum to work with the result as an array
D=struct2cell(dir('*.txt'))
file=D(2,:)'
SORT=sort(file);
DATENUMBER=datenum(SORT);
and that is the final result I wished.
Thank you again for investing your time to help.
Wish you a nice day
Emerson
ok then sort our array
d=datevec(file)
f=datestr(sortrows(d,[1:6]))
@Azzi: The order of fields replied by DIR is not documented. Although I assume, that TMW will not change the order, using the field names is safer:
D = dir('*.txt');
Dates = {D.date};
Sorting the numerical date numbers is safer, because it does not fail when the day, month or year changes:
index = sort([D.datenum]);

Sign in to comment.

More Answers (1)

Which date and time do you mean? The creation date, the date of the last modification or the last access? The dir command replies the last modification date. To get the creation date, you can use FEX: FileTime under Windows:
D = dir(fullfile(Folder, '*.txt')); % Absolute files names are safer
Files = {D.name};
Dates = cell(1, length(Files));
for iFile = 1:length(Files)
Dates{iFile} = GetFileTime(fullfile(Folder, Files{iFile}), ...
'Local', 'Creation');
end
If the modification date is ok, this is simpler:
D = dir(fullfile(Folder, '*.txt')); % Absolute files names are safer
Files = {D.name};
Dates = {D.date};
Then the Files are sorted alphabetically. If you want a chronological sorting:
[dummy, index] = sort([D.datenum]);
Files = Files(index);
Dates = Dates(index);

3 Comments

Hi Jan, thanks for your suggestions. The creation date is the one of interest. So I tried your first version as follow:
D = dir(fullfile('C:\Users\EMERSON\Desktop\TEST\', '*.txt')); % Absolute files names are safer
Files = {D.name};
Dates = cell(1, length(Files));
for iFile = 1:length(Files)
Dates{iFile} = GetFileTime(fullfile(C:\Users\EMERSON\Desktop\TEST\, Files{iFile}), 'Local', 'Creation');
end
but then I obtain the following error message:
Error: File: Untitled.m Line: 5 Column: 41
Unexpected MATLAB operator.
The folder name worked without problem in the first line, but not in line 5. If I use 'path', then again I obtain another error.
Do you know what is wrong with the syntax?
Thank you for helping
Emerson
Simon, I downloades your submission GetFileTime.m, but it's empty (just comments)
@Emerson: You forgot the quotes around the folder name. It will be more flexible to use a variable called "Folder" instead of hardcoding the folder in the code.
@Azzi: Then please read the comments. There you find instruction about the compilation of the included C-files. Unfortunately pre-compiled MEX-files are not allowe in the FileExchange, but you find a link to my homepage, where you can download pre-compiled files.

Sign in to comment.

Categories

Products

Community Treasure Hunt

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

Start Hunting!