listing folders in directory with specific strings

78 ビュー (過去 30 日間)
Jorge Rivé
Jorge Rivé 2018 年 8 月 16 日
編集済み: Jorge Rivé 2018 年 8 月 17 日
I have a directory full of folders. I want to only list (and count) those with a specific naming format that starts with a numerical string and not any other folders in that directory. How can I do this? for example: FolderA contains these subfolders
180705-France, 180705-Germany, refdata
I want to list and count only the folders under folder A that start with a numeric string. In this case, the list should return '180705-France' and '180705-Germany', and a length(dirlist) should return 2.
something like,
pathn =['D:\FolderA'];
list= dir([pathn '\1*']);
len =length(list);
would work, but I need it to work for any folder whose name begins with numbers 0-9.
I tried using regexp but couldn't figure out how to get it to work. If I figure it out, I'll post it here, but I'm sure someone already knows how to do this....thanks.
  2 件のコメント
Stephen23
Stephen23 2018 年 8 月 17 日
編集済み: Stephen23 2018 年 8 月 17 日
Note that these square brackets do nothing at all:
pathn =['D:\FolderA'];
Square brackets are a concatenation operator, and that code is not concatenating anything together (the char vector is already a char vector). All that is needed is:
pathn = 'D:\FolderA';
Jorge Rivé
Jorge Rivé 2018 年 8 月 17 日
yes, thanks --I know...I started writing the question one way and then simplified it to avoid confusion. The path was a concatenation of strings before I changed it....

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

採用された回答

Paolo
Paolo 2018 年 8 月 16 日
編集済み: Paolo 2018 年 8 月 16 日
You can work around it with a dos command:
[status,cmdout] = dos("dir *")
list = regexp(cmdout,'\d{6}-[A-Za-z]+','match');
len =length(list);
or alternatively:
s = dir;
list =regexp({s.name},'\d{6}-[A-Za-z]+','match');
len = length(find(~cellfun(@isempty,list)));
  1 件のコメント
Jorge Rivé
Jorge Rivé 2018 年 8 月 17 日
編集済み: Jorge Rivé 2018 年 8 月 17 日
thank you, the dos command version worked, but the alternative provided does not. list returns an array of empty cells.... Your first solution does the trick for me -- Thanks.

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2018 年 8 月 16 日
dir() does not have the regexp option. I can think of this way
a=dir;
b=char({a([a.isdir]).name});
len=sum(isstrprop(b(:,1),'digit'))

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by