Group files with same names

I have a folder with images of different models of cars(20 to be specific). For each car model I have 100 sample images. They are all in a single folder and named:
Audi_01, Audi_02, ...... , Audi_100
Lexus_01, Lexus_02 ...., Lexus_100
...
How can I group same model cars and put them in a new folder.
I tried : copyfile Audi*.jpg Audi
But, for that I have to type and run the command for 20times.
Is there any alternative way or command in matlab?

回答 (1 件)

Jan
Jan 2021 年 6 月 7 日
編集済み: Jan 2021 年 6 月 7 日

0 投票

Folder = 'D:\Your\Data\Folder';
FileList = dir(fullfile(Folder, '*.jpg'));
FileName = {FileList.name};
Name = unqiue(strtok(FileName, '_'));
for iName = 1:numel(Name)
mkdir(fullfile(Folder, Name{iName}));
match = startsWith(FileName, [Name{iName}, '_']);
Group = FileName(match);
for iGroup = 1:numel(Group)
copyfile(fullfile(Folder, Gorup{iGroup}), fullfile(Folder, Name{iName}));
end
end

4 件のコメント

HPL
HPL 2021 年 6 月 8 日
This code almost worked.
I have a several groups named in a different way, like
Chevrolet_camaro_01, 02,03....
and
Chevrolet_Colorado_01,02,03
But, according to the code, "FileName" variable takes both of them in a single variable "Chevrolet"
Stephen23
Stephen23 2021 年 6 月 8 日
編集済み: Jan 2021 年 6 月 9 日
Try:
Name = unique(regexprep(FileName,'_*\d+$',''));
HPL
HPL 2021 年 6 月 16 日
This code isn't working.
"Name" is storing all the file names rather than storing unique filenames
The error I am getting is:
Unable to create directory.
  • " *.jpg " already exists as a file.
Stephen23
Stephen23 2021 年 6 月 17 日
@HPL: please save the cell array FileName in a .mat file and upload it by clicking the paperclip button.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

リリース

R2020b

質問済み:

HPL
2021 年 6 月 7 日

コメント済み:

2021 年 6 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by