Join filename and date from dir output

12 ビュー (過去 30 日間)
Matt
Matt 2019 年 6 月 7 日
コメント済み: Walter Roberson 2019 年 6 月 7 日
How can I join the filenames and dates together from a dir structure output?
I have used dir, and I am ordering the filenames alphabetically, or by date, and then putting these filenames in this order in a listbox.
But, I would like to provide the user more info about the file in the listbox - particularly the file date.
Have tried several methods but can only join the filenames one after the other THEN the dates one after the other together, rather than join each date to each filename.
Current code:
handles.folder = 'C:\User Files';
handles.fileList = dir(fullfile(handles.folder, '*.mat')); % list of no logical order (name, date or size)
handles.fileList = handles.fileList(~[handles.fileList.isdir]); % get files only
handles.uppercaseFileList = upper([{handles.fileList.name}]); % convert to filenames to uppercase
[sorteduppercasenames idx] = sort(handles.uppercaseFileList); % Sort uppercase filenames - default - ascending
handles.fileList = handles.fileList(idx); % retrieve original filenames (not purely uppercase) in sorted order using index
set(handles.FileListBox,'String',{handles.fileList.name}); % update listbox with ordered filenames
Looking for format:
"File1.mat | 31-May-2019 14:27:19"
"File2.mat | 07-Jun-2019 22:44:35"
Unless there is a better way to build a file manager directly on the GUI (rather than uigetfile)...? So far I have it working so that the data is loaded into the GUI when the user selects a file from the listbox. I have added in functionality to delete and rename the files, and sort alphabetically (case insensitive) ascending and then descending when filename button pushed again, and date descending, then ascending when date button pushed again. I'd just like to be able to show the user the file dates now!
  2 件のコメント
per isakson
per isakson 2019 年 6 月 7 日
Matt
Matt 2019 年 6 月 7 日
Thanks, i'll take a look at that!

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 6 月 7 日
display_str = strjoin( handles.fileList.name, {' | '}, handles.fileList.date );
st(handles.FileListBox, 'String', display_str);
note: you need the {' | '} to be a cell array, not just the plain character vector ' | ', or else the leading and trailing spaces will automatically be trimmed out.
  4 件のコメント
Matt
Matt 2019 年 6 月 7 日
編集済み: Matt 2019 年 6 月 7 日
Hi Walter. Thanks. This produces what I managed myself - a continuous string - unless I am missing something? This gives me a listbox with one single line:
"File1.matFile2.matFile3.mat......... | 31-May-2019 14:27:1907-Jun-2019 22:44:3507-Jun-2019 23:24:15........"
Rather than seperate lines:
"File1.mat | 31-May-2019 14:27:19"
"File2.mat | 07-Jun-2019 22:44:35"
....
"File3.mat | 07-Jun-2019 23:24:15"
This is the code.
% Fill file browser table
handles.folder = 'C:\User Files';
handles.fileList = dir(fullfile(handles.folder, '*.mat')); % list of no logical order (name, date or size)
% CASE SENSITIVE
handles.fileList = handles.fileList(~[handles.fileList.isdir]); % get files only
handles.uppercaseFileList = upper([{handles.fileList.name}]); % convert to filenames to uppercase
[sorteduppercasenames idx] = sort(handles.uppercaseFileList); % Sort uppercase filenames - default - ascending
handles.fileList = handles.fileList(idx); % retrieve original filenames (not purely uppercase) in sorted order using index
display_str = strcat(handles.fileList.name, {' | '}, handles.fileList.date);
set(handles.FileListBox,'String',display_str);
Walter Roberson
Walter Roberson 2019 年 6 月 7 日
display_str = strcat( {handles.fileList.name}, {' | '}, {handles.fileList.date} );
st(handles.FileListBox, 'String', display_str);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by