Borrar filtros
Borrar filtros

Searching image files with a particular keyword within a folder

8 visualizaciones (últimos 30 días)
ch basit
ch basit el 11 de Mzo. de 2011
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.

Respuestas (2)

Paulo Silva
Paulo Silva el 11 de Mzo. de 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
  1 comentario
ch basit
ch basit el 11 de Mzo. de 2011
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.

Iniciar sesión para comentar.


Paulo Silva
Paulo Silva el 11 de Mzo. de 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

Categorías

Más información sobre Import, Export, and Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by