필터 지우기
필터 지우기

grouping files together by name

조회 수: 10 (최근 30일)
MJ
MJ 2021년 8월 11일
댓글: MJ 2021년 8월 11일
I would like to iterate through a list of PNG files in a folder (inputdir) and group them based on their file name.
The png filenames end in a format like xxx.01.001.png, xxx.01.002.png... xxx.01.256.png, where xxx is irrelevant.
and I would like to group and move the 256 png files into one folder in a directory like inputdir/01
And repeat the same for other groups like xxx.02.001.png, xxx.03.001.png and so forth
How would this be accomplished? I am trying to code the solution but I am stuck.
cd inputdir
S = dir(fullfile(inputdir,'*.PNG'));
for k = 1:numel(S)
fnm = fullfile(inputdir,S(k).name);
newj = % some code here
if j ~= newj
mkdir(string(newj));
j = newj;
end
end

채택된 답변

Image Analyst
Image Analyst 2021년 8월 11일
You'll need to parse the filenames then sort them by whatever number you want to sort them by. You can use fileparts() and strsplit() to break the filename into parts. Here is a start:
S = dir(fullfile(inputdir,'*.PNG'))
allFileNames = {S.name};
for k = 1 : length(allFileNames)
thisName = allFileNames{k};
[~, baseFileNameNoExt, ext] = fileparts(thisName);
if ~contains(baseFileNameNoExt, '.')
continue; % Skip names with no dot in them.
end
fprintf('Processing %s.\n', baseFileNameNoExt);
nameParts = strsplit(baseFileNameNoExt, '.')
end
  댓글 수: 1
MJ
MJ 2021년 8월 11일
Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Simon Chan
Simon Chan 2021년 8월 11일
Following code assumes all your png files are inside the working directory and the filename format are xxxx.00.000.png.
clear; clc;
myFolder = 'C:\Users'; % Your working directory
S = dir(fullfile(myFolder,'*.png'));
filename = {S.name};
filename_split = cellfun(@(x) strsplit(x,'.'),filename,'UniformOutput',false);
filename_combine = vertcat(filename_split{:});
foldername = unique(filename_combine(:,2));
cellfun(@(x) mkdir(fullfile(myFolder,x)),foldername);
cellfun(@(x,y) movefile(fullfile(myFolder,x),fullfile(myFolder,y)),filename,filename_combine(:,2)');

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by