Searching image files with a particular keyword within a folder

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.

Answers (2)

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

1 Comment

Its working perfect but what if I want to sort all images with different keywords e.g 'C11' 'D12' 'F13' in their respective folders by just running the code once.

Sign in to comment.

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

Categories

Find more on Images in Help Center and File Exchange

Tags

Asked:

on 11 Mar 2011

Community Treasure Hunt

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

Start Hunting!