Access multiple folders and extract specific files

Hey everyone,
I need to access to different files both type ".jp2" and I would like to create a code in a loop which would extract those two files. My problem is how to actually access those files I need:
I have 12 folders with different names. Inside each of these 12 folders there are 10 items, 5 of them are folders. I need to access only of those 5 folders which is called "GRANULE". Inside that folder there's only which I also need to access (and its name changes according to the main folder). Clicking in that folder, I have other 4 items, 3 of them are folders and need to access the one called "IMG_DATA". Inside that one there are 3 folders and I need to access the one called "R10m" (this name is equal for all 12 main folders). Finally, inside the last folder, the two files I need to extract end in "(...)B04_10m.jp2" and "(...)B08_10m.jp2"
Now, I would like to create a code which would access each of these 12 main folders and extract the two ".jp2" files I need.
I have tried several codes but none of them seem to work for me. Can you help me, please?
I hope I was able to explain the problem clearly... Thank you in advance!

Answers (2)

Hello,
If I understood you well, the code below could help you
clear
root_folder = 1:12;
fig_number = [4 8];
for i = 1:12
for j = 1:2
filename = sprintf('name_of_folder%d/GRANULE/IMG_DATA/R10m/(name_of_figure)B0%d_10m.jp2',...
root_folder(i),fig_number(j))
end
end
Assume, you have 12 folders and you want to access two figures in each folder. You could adapt the code for you case.

3 Comments

MLM
MLM on 3 Jul 2019
Edited: MLM on 3 Jul 2019
Trung VO DUY, thank you for your answer! I'm not too experienced in Matlab so maybe by seing this printscreen I can make myself clear... those two files in blue are the ones I want to get to. The problem, for me, is how to access them because they folders inside folders, etc. And some of the folders change names, according to the main folder (not the 29TPE but the other "below" it). And I have to do this for each folder inside the 29TPE (therefore, I thought of creating a loop, but hasn't been working specially because some folder names' change and I don't know how to change them inside the loop).
Thank you!
Hello,
You can refer the answer of @ Guillaume. It could help.
MLM
MLM on 3 Jul 2019
Edited: MLM on 3 Jul 2019
Hi,
I had already tried that but the answer appears empty and I cannot understand why.
I'll keep searching, though. Thank you for your help!

Sign in to comment.

Doesn't sound particularly complicated. For a while now, dir has been able to recurse into folders, so a single
filelist = dir(fullfile(rootfolder, '**', '*B0?_10m.jp2')) %** tells dir to look in all the subfolders not matter how deep they are
should give you the list of all relevant jp2 files in any subdirectory of rootfolder.
From there, it would be easy to identify which directory they're actually in, but it's not clear what the actual structure is and what you're actually interested in, so can't give you code for that yet.

7 Comments

Hi,
I had already tried that but the answer appears empty and I cannot understand why.
I'll keep searching, though. Thank you for your help!
I had already tried that
Tried what exactly? Give us the exact code you've tried
but answer appears empty
What does that mean? Again, show us the exact output.
It's not easy to help you when you're so vague about what you're doing and what's happening.
I'm sorry, you're right. What I meant was I had:
mainfolder = 'C:\Users\me\Sentinel2_NDVI\29TPE';
files_jp2 = dir(fullfile(mainfolder, '*.jp2'));
And the result was:
files_jp2 --> 0x1 struct (with 5 fields) but there's nothing in each field. No information.
I find that when there's a *.jp2 file in the main folder it recognizes it, but not when it is in subfolders.
So, you didn't try what I said, so it's no wonder it didn't work. You removed te crucial part, the '**' which tells matlab to go look in subfolders.
mainfolder = 'C:\Users\me\Sentinel2_NDVI\29TPE';
files_jp2 = dir(fullfile(mainfolder, '**', '*.jp2')); % ** ESSENTIAL!
I've posted my last attempt when running the code but I have tried both ways (with '**' and without) and none of them worked. The result I get is always the same:
files_jp2 --> 0x1 struct (with 5 fields) but there's nothing in each field. No information.
Could it be because of the matlab version I'm using? I'm curruntly working with Matlab2016a.
Could it be because of the matlab version I'm using? I'm curruntly working with Matlab2016a.
Yes, that's crucial information that should have been mentioned. There's a field for the version you're using near the top right of the page.
When I said, "For a while now, dir has been able to recurse into folders", the while started at R2016b. With 2016a, it's more complicated, you'll have to implement the recursion yourself.
If you have the option to upgrade, at least to 2016b, that would make your life a lot easier. If not, you'll have to wait until tonight when I may have more time to dedicate to this.
OK, thank you for the info. I'll find another way to get those files, don't waste anymore time on this question. Thank you for your help!

Sign in to comment.

Categories

Asked:

MLM
on 3 Jul 2019

Commented:

MLM
on 4 Jul 2019

Community Treasure Hunt

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

Start Hunting!