How to organize text files by date and time inside folder

4 次查看(过去 30 天)
Hello, I have many txt files inside a folder. the txt files had the name format: xxx_xxx_DayName-Number-Month-Year_HH-MM-SS.txt. I need to organize those files by date and time as wrote on the file name from the older to the newest, please help me to solve this problem. Thank you. Hicham ZATLA
  1 个评论
Stephen23
Stephen23 2016-4-11
编辑:Stephen23 2016-4-11
The simplest solution is to rename your files and use an ISO 80601 date, then the files will sort into chronological order using any standard text sort (including the OS's folder display). Any other solution is going to be much more complicated and buggy than if you simply named the files using the world's best date format: ISO 8601.
Sorting ISO 8601 dates is simple because you don't need to do anything special at all:
>> C = {'2016-02-04';'1999-03-31';'1999-03-29';'2005-05-25'}
C =
2016-02-04
1999-03-31
1999-03-29
2005-05-25
>> sort(C)
1999-03-29
1999-03-31
2005-05-25
2016-02-04
This is a very good example of where good planning makes writing code much easier, and where bad design choices make code more complicated and buggy. You made a choice of filenames that is going to be much more difficult and buggy to sort than if you had used a simple ISO 8601 date format.

请先登录,再进行评论。

回答(2 个)

Muhammad Usman Saleem
编辑:Muhammad Usman Saleem 2016-4-11
  1 个评论
Stephen23
Stephen23 2016-4-13
None of these help with sorting date strings with the order day-month-year. Even if the sort used a natural order sort on the numeric values within the strings, the output would still not be in chronological order.
None of these suggestions actually answer the original question: "I need to organize those files by date and time as wrote on the file name from the older to the newest" They would, at best, sort by numeric values within the strings, which is not the same as the chronological order for the date format used in those date strings.
See my answer for one way to correctly sort date strings into chronological order.

请先登录,再进行评论。


Stephen23
Stephen23 2016-4-11
编辑:Stephen23 2016-4-11
% Create some fake filenames:
V = now - [0:0.987654321:10].';
fmt = 'DDDD-DD-mm-yyyy_HH-MM-SS';
C = cellstr(datestr(V,fmt));
C = strcat('xxx_xxx_',C,'.txt');
C(randperm(numel(C))) = C % randomize!
creates this cell array of filenames, with the dates in a random order:
C =
xxx_xxx_Saturday-02-04-2016_20-44-14.txt
xxx_xxx_Monday-04-04-2016_20-08-40.txt
xxx_xxx_Sunday-10-04-2016_18-22-00.txt
xxx_xxx_Saturday-09-04-2016_18-39-47.txt
xxx_xxx_Thursday-07-04-2016_19-15-20.txt
xxx_xxx_Wednesday-06-04-2016_19-33-07.txt
xxx_xxx_Friday-01-04-2016_21-02-00.txt
xxx_xxx_Tuesday-05-04-2016_19-50-54.txt
xxx_xxx_Friday-08-04-2016_18-57-34.txt
xxx_xxx_Monday-11-04-2016_18-04-14.txt
xxx_xxx_Sunday-03-04-2016_20-26-27.txt
Now to sort them into chronological order we can do this:
D = regexp(C,'[^_]+_[^_]+_(.*)\.txt','once','tokens');
N = datenum(vertcat(D{:}),fmt);
[~,X] = sort(N);
out = C(X)
to get the sorted cell array of filenames:
out =
xxx_xxx_Friday-01-04-2016_21-02-00.txt
xxx_xxx_Saturday-02-04-2016_20-44-14.txt
xxx_xxx_Sunday-03-04-2016_20-26-27.txt
xxx_xxx_Monday-04-04-2016_20-08-40.txt
xxx_xxx_Tuesday-05-04-2016_19-50-54.txt
xxx_xxx_Wednesday-06-04-2016_19-33-07.txt
xxx_xxx_Thursday-07-04-2016_19-15-20.txt
xxx_xxx_Friday-08-04-2016_18-57-34.txt
xxx_xxx_Saturday-09-04-2016_18-39-47.txt
xxx_xxx_Sunday-10-04-2016_18-22-00.txt
xxx_xxx_Monday-11-04-2016_18-04-14.txt
  2 个评论
Stephen23
Stephen23 2016-4-12
ZATLA Hicham's "Answer" moved here:
Thank you all for your reply. I think my question it was not clear, I want to sort the txt files _ inside the folder (example datas)_ not only sort them in the output name of Matlab, so when the Matlab give me the first name txt file I want also to copy this file and past it in the folder now the same for the next file with copy and past the file after the first file inside the folder and so on.
Stephen23
Stephen23 2016-4-12
编辑:Stephen23 2016-4-12
@ZATLA Hicham: The sorting of the files inside the folder has nothing to do with MATLAB: this depends on your operating system or file manager.
The simplest solution (which is most likely to work) is exactly as I explained in my comment to your question: use an ISO 8601 date format. Then probably most OS's will display the dates in the correct order.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by