Searching image files with a particular keyword within a folder
8 views (last 30 days)
Show older comments
I am trying to search image files within a folder containing a particular key word for example i have images with such names
ABC 1-4, ABC_C11_1_2010y10m23d_02h00m.tif ABC 1-4, ABC_G11_1_2010y10m23d_02h00m.tif
The only difference between these file names is C11 AND G11. I want to search the entire folder and pick all such images with keyword C11 and transfer them to a folder having the name same as the keyword C11. Then look for images with G11 keyword and transfer them in a new folder and so on.
Any help would be appreciated.
0 Comments
Answers (2)
Paulo Silva
on 11 Mar 2011
This code should work for MS Windows OS, it finds the files containing the string you specify and copies them to another directory, in this case is just a directory inside the one containing the files, for other OS adapt the code.
FolderPath=''; %insert the path inside those ''
FileNames=dir(FolderPath);
ContainString='C11'; %string you are interested in
NumFiles=size(FileNames,1);
for a=1:NumFiles
FileDetail=FileNames(a,:);
if ((~isempty(strfind(FileDetail.name,ContainString))) & ~FileDetail.isdir)
FilesInSub=dir([FolderPath '\' ContainString]);
if isempty(FilesInSub)
mkdir([FolderPath '\' ContainString]);
end
copyfile([FolderPath '\' FileDetail.name],[FolderPath '\' ContainString '\' FileDetail.name] ,'f');
end
end
Paulo Silva
on 11 Mar 2011
FolderPath='C:\Users\PJMDS\Desktop\TesteFicheiros';
FileNames=dir(FolderPath);
ContainStrings={'C11','G11'}; %insert here the string you are looking for
NumFiles=size(FileNames,1);
for b=1:numel(ContainStrings)
ContainString=char(ContainStrings(b));
for a=1:NumFiles
FileDetail=FileNames(a,:);
if ((~isempty(strfind(FileDetail.name,ContainString))) & ~FileDetail.isdir)
FilesInSub=dir([FolderPath '\' ContainString]);
if isempty(FilesInSub)
mkdir([FolderPath '\' ContainString]);
end
copyfile([FolderPath '\' FileDetail.name],[FolderPath '\' ContainString '\' FileDetail.name] ,'f');
end
end
end
See Also
Categories
Find more on Import, Export, and Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!