Access multiple folders and extract specific files
14 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
infinity
2019-7-3
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 个评论
Guillaume
2019-7-3
编辑:Guillaume
2019-7-3
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 个评论
Guillaume
2019-7-4
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!