Access multiple folders and extract specific files
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (2)
infinity
am 3 Jul. 2019
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 Kommentare
Guillaume
am 3 Jul. 2019
Bearbeitet: Guillaume
am 3 Jul. 2019
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 Kommentare
Guillaume
am 4 Jul. 2019
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.
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!