Searching image files with a particular keyword within a folder

8 次查看(过去 30 天)
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.

回答(2 个)

Paulo Silva
Paulo Silva 2011-3-11
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 个评论
ch basit
ch basit 2011-3-11
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.

请先登录,再进行评论。


Paulo Silva
Paulo Silva 2011-3-11
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

类别

Help CenterFile Exchange 中查找有关 Import, Export, and Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by