MATLAB Answers

If I have a logical vector created by ISDIR attribute of DIR, how to have its order by the date of the last modification of folders???

9 ビュー (過去 30 日間)
Kapa Kudaibergenov
Kapa Kudaibergenov 2019 年 12 月 1 日
Commented: Walter Roberson 2019 年 12 月 2 日
d = dir('D:\= BIO-PD =');
isub = [d(:).isdir]; %# returns logical vector
nameFolds = {d(isub).name}';
In nameFolds I have a cell array with the names of folders contained in 'D:\= BIO-PD ='.
The problem is I need to have nameFolds sorted by the date of the last change of the folders, as it is located in the real folder.

  2 件のコメント

Walter Roberson
Walter Roberson 2019 年 12 月 1 日
Look at the second output of sort, and use that to reorder the other content
Kapa Kudaibergenov
Kapa Kudaibergenov 2019 年 12 月 1 日
Can you explain me how we use it in this case? Do you mean to use it like:
[B,I] = sort(nameFolds)

サインイン to comment.

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2019 年 12 月 1 日
編集済み: Stephen Cobeldick 2019 年 12 月 1 日
Simpler and much more robust:
S = dir('D:\= BIO-PD =');
S = S([S.isdir] & ~ismember({S.name},{'.','..'})); % folders only, exclude '.' and '..'
[~,X] = sort([S.datenum]); % sort datenum
N = {S(X).name} % folder names in datenum order

More Answers (1)

Image Analyst
Image Analyst 2019 年 12 月 1 日
Try this.
files = dir('*.*')
areAFolder = [files.isdir]
files = files(areAFolder) % Get folders only, not files.
% Get rid of dot and dot dot folders.
if length(files) >= 3
% Assume they are the first two entries. Remove them.
files = files(3:end);
% % OR Alternate way that does not depend on them being the first two. Comment out the line above if you use this method.
% [ia, ib] = ismember({'..'}, {files.name})
% files(ib) = [];
% [ia, ib] = ismember({'.'}, {files.name})
% files(ib) = [];
end
[fileDates, sortOrder] = sort([files.datenum], 'descend') % or 'ascend' - whatever you want.
folders = files(sortOrder)
Adapt as needed.

  7 件のコメント

表示 4 件の古いコメント
Walter Roberson
Walter Roberson 2019 年 12 月 2 日
I have not tested far enough in Windows to determine how NTFS sorts filenames in practice. For example if the user used U+0308 (combining diaerisis) followed by lower case o then will NTFS canonicalize as ö U+00F6? Will it use utf8 and sort as 0xCC 0x88? Will it use utf16 little endian and sort as 0x08 0x03? Will it use utf16 big endian and sort as 0x03 0x08? Will it sort by the 8+3 surrogate name?
Image Analyst
Image Analyst 2019 年 12 月 2 日
I think if you sorted by date before removing the dot and dot dot, then they should always be the first two in the list, because as far as I know they must always be the oldest (unless it's something to do with Windows multiple dates policy). That's what I had intended to do, but didn't. But of course checking the name directly is more robust.
Walter Roberson
Walter Roberson 2019 年 12 月 2 日
The field returned by dir() is modification date, and modification date of a directory reflects the most recent time a file was added or removed from the directory. That would tend to sort . at the end possibly some distance from ..

サインイン to comment.

サインイン してこの質問に回答します。

タグ

製品


リリース

R2018b

Translated by