How to organize text files by date and time inside folder
조회 수: 9 (최근 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
댓글 수: 0
답변 (2개)
Muhammad Usman Saleem
2016년 4월 11일
편집: Muhammad Usman Saleem
2016년 4월 11일
Also read for more help
댓글 수: 1
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
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
2016년 4월 12일
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
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 Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!