How to list the directories of multiple folders?

9 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Rik
Rik il 25 Feb 2022
base_path='C:\Users\Desktop\Stats\';
P=cell(5,1);
for n=1:numel(P)
P{n}=sprintf('%sID%03d_Stats',base_path,n);
end
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'}

Più risposte (1)

Image Analyst
Image Analyst il 25 Feb 2022
To get all (say) workbooks under 'C:\Users\Desktop\Stats' and all its subfolders
do
filePattern = 'C:\Users\Desktop\Stats\**\*.xlsx';
fileList = dir(filePattern)
  4 Commenti
Image Analyst
Image Analyst il 25 Feb 2022
dpb, yes that would be nice, like
fileList = dir(filePattern, 'IncludeSubfolders', true);
I'll suggest it to the developers.
dpb
dpb il 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.
C:\Users\Duane>
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.
ADDENDUM:
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.

Accedi per commentare.

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by