Listing files in an arbitrarily named and deep tree

Hey all,
I would like to list all the .avi files in all levels of depth of subdirectories, starting from a parent path. The naming and the level of depth of the directories are arbitrary.
For example, if I give C:\banana\ as a parentpath, with the following structure:
C:\banana\apple.avi
C:\banana\pear\kiwi\banana.avi
C:\banana\orange\kiwi.avi
I would like to get all the paths to the above mentioned avis. From what I understand from the documentation there is no built-in function which gives me this functionality.
genpath seems quite promising though, in that it gives me all the directories in all levels of depth starting from a parent path.
The downside is that it gives me a string, in which all the paths are separated by a semi-colon.
I guess I have to combine the functionality of genpath and ls. I would have to make a list from the string given by genpath, turn it into an array of strings which would in turn be put in ls through the use of a for-loop.
I can than evaluate the resulting ls lists on the presence of avi's.
This solution however doesn't feel as straightforward as it could be. Is there a functionality of MATLAB I have overlooked?
Any suggestions are much appreciated!

 採用された回答

Jan
Jan 2011 年 10 月 10 日

0 投票

1 件のコメント

Bert
Bert 2011 年 10 月 10 日
I've been quite cautious about using FEX in my project, because I feel the reliability of these algorithms cannot be guaranteed.
I guess I have to overcome my 'fear' of FEX.
Judging from the amount of proposed algorithms, I would think that Mathworks might want to look into adding this functionality to MATLAB.

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

その他の回答 (1 件)

Matt Tearle
Matt Tearle 2011 年 10 月 10 日

0 投票

Looks like you're on a Windows system, so try:
[~,flist] = system('dir *.avi /B/S')
You may want to cellstr the result.
ETA: Actually, don't use cellstr. flist is in the form of a single line (with \n characters in it). So use regexp instead:
fileList = regexp(flist,'\n','split');
Note that the last entry will be blank, so
fileList(end) = [];

2 件のコメント

Bert
Bert 2011 年 10 月 10 日
Thanks for your reply.
I tried using your solution as follows:
pathName = uigetdir;
command = ['dir ',pathname,'\ *.avi /B/S'];
[~,flist] = system(command);
fileList = cellstr(flist);
However, I get the following string in fileList: The system cannot find the file specified.
Matt Tearle
Matt Tearle 2011 年 10 月 10 日
A couple of things: make sure you're using pathName (with the capital N) to make the command, and get rid of the space between the \ and the *.avi. But also I didn't notice the format that flist has, which means that cellstr won't work. See edit for solution.

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

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

質問済み:

2011 年 10 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by