How to list the directories of multiple folders?

3 views (last 30 days)
Tomaszzz on 25 Feb 2022
Commented: dpb on 25 Feb 2022
Hi all,
I have a number of folders that I would like to list and later access the files from them. Currently, I do it in this way:
P1 = 'C:\Users\Desktop\Stats\ID001_Stats';
P2 = 'C:\Users\Desktop\Stats\ID002_Stats';
P3 = 'C:\Users\Desktop\Stats\ID003_Stats';
P4 = 'C:\Users\Desktop\Stats\ID004_Stats';
P5 = 'C:\Users\Desktop\Stats\ID005_Stats';
Is there an easier way to do this since I have 20 folders?

Accepted Answer

Rik on 25 Feb 2022
for n=1:numel(P)
P = 5×1 cell array
{'C:\Users\Desktop\Stats\ID001_Stats'} {'C:\Users\Desktop\Stats\ID002_Stats'} {'C:\Users\Desktop\Stats\ID003_Stats'} {'C:\Users\Desktop\Stats\ID004_Stats'} {'C:\Users\Desktop\Stats\ID005_Stats'}

More Answers (1)

Image Analyst
Image Analyst on 25 Feb 2022
To get all (say) workbooks under 'C:\Users\Desktop\Stats' and all its subfolders
filePattern = 'C:\Users\Desktop\Stats\**\*.xlsx';
fileList = dir(filePattern)
dpb on 25 Feb 2022
@Rik, but that is system dependent syntax not known to the normally-used Windoes shells, so it isn't truly platform-agnostic in that to know it already one has to be familiar with **nix (and, as noted in your link, even there, precise meaning is dependent upon which shell one is using).
For the normal CMD that Windows users will be familiar with (if they're familiar with command line tools at all, any more, which is becoming less and less common), one finds
C:\Users\Duane>dir c:\Users\Duane\**\*.x*
The filename, directory name, or volume label syntax is incorrect.
so TMW has built that idiom into dir. I do believe I recall it is at least documented in one of the Tips or use case notes, but having to go read the doc for something as common as DIR() seems totally counter to the idea of a rapid-development language.
OTOH, either having named parameters as @Image Analyst posits (workable, but verbose and dependent upon which ones are chosen to be implemented) or my suggestion of just passing switches with their expected meaning to the underlying OS gives it back to the user to be at their level of comfort/experience on their platform.
Granted, if one is writing production code or in an environment in which code must operate on more than one OS, then my suggestion would probably require the developer to make allowances for the given OS. But, they can always choose to use whatever tools TMW does support in that case and go for the lowest common denominator solution.
I'll note that I also am in the small minority that use a substitute shell under Windows (JPSoft Take Command; shameless plug for them although totally unaffiliated) which has a much expanded batch language, many more builtin commands and options for those. I am in a situation where I don't have to worry about cross platform development so I use those features extensively when they make what I'm after simpler.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!