Searching image files with a particular keyword within a folder
4 次查看(过去 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.
0 个评论
回答(2 个)
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
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 Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!