Matlab list all files in subfolders of the same name...
이전 댓글 표시
Hi, all:
My file/directory structure is as follows:
tests
----dir1
--------sweep
------------results.txt
----dir2
--------sweep
------------results.txt
----dir3
--------sweep
------------results.txt
...
----dirN
--------sweep
------------results.txt
I'd love to list all "results.txt" in all subfolders named "sweep". As you may have noticed, dir1, dir2, ... dirN are different.
Can anybody give me a hand? Thanks...
Cheers Pei
댓글 수: 1
Paulo Silva
2011년 8월 2일
what have you done so far in order to do what you want?
답변 (5개)
the cyclist
2011년 8월 2일
ls */sweep/results.txt
댓글 수: 8
Pei JIA
2011년 8월 2일
Ashish Uthama
2011년 8월 2일
This should have worked if you were in the 'tests' directory.
the cyclist
2011년 8월 2일
When you say it "failed", do you mean it gave an error, or an unexpected result? I am very confident that you should be able to get the "ls" command to do what you want.
Walter Roberson
2011년 8월 2일
Perhaps JIA needs the specific
ls tests/*/sweep/results.txt
Pei JIA
2011년 8월 3일
Fangjun Jiang
2011년 8월 3일
I wonder if you tried the which command. The addpath() command adds all your folder and subfolder into MATLAB path temporarily. The which command should tell you all the files found.
Pei JIA
2011년 8월 3일
Fangjun Jiang
2011년 8월 4일
That is weird. Do you have lots of sub-folders under \tests\? It works slow for me too but it took 2 seconds to generate a path string totaling 77k characters. Anyway, hope you know the function genapth(), addpath() and which() now.
Fangjun Jiang
2011년 8월 2일
addpath(genpath(pwd));
which results.txt -all
or, function format of which()
files=which('results.txt','-all');
Walter Roberson
2011년 8월 2일
0 개 추천
See my answer in the similar Question http://www.mathworks.com/matlabcentral/answers/12233-finding-a-string-in-a-file
Pierre
2011년 8월 4일
Unfortunately I'm not allowed to post my solution to a similar problem, but I can roughly summerize my approach: I made a function which looks like
myRecursiveDir(root, varargin);
and in your case you would call it like
myRecursiveDir('tests', 'dir*', 'sweep', 'results.txt');
The method then recursively fetches the list of directories/files at each level with the dir command and returns the composed filenames.
Hope this might help.
댓글 수: 2
Fangjun Jiang
2011년 8월 4일
I am curious to know why the built-in function genpath() and which() can not do the job. Thanks!
Pierre
2011년 8월 5일
Well, depending on the structure of your directory-tree, the number or files/folders besides the ones you want walk-through, and the frequence you query for files, genpath() becomes quite inefficient, as the size of your list of file names grows exponentially in terms of searching depth.
In our case it was definitely worth the effort to invest a quarter of an hour to implement that kind of 'guided' search.
But you are right, genpath() should work for less complex and huge directory trees. When I first read your answer I only remembered 'genpath is not suitable' (it's been a while since we were faced to that issue), I'm sorry for that. ;)
Oleg Komarov
2011년 8월 4일
rdir('C:\tests\**\sweep\results.txt')
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!