I have a folder that contain 10 subfolders each subfolder contains 4 images named 1.bmp,2.bmp,3.bmp,4.bmp, I want to rename those images to 9.bmp,10.bmp,11.bmp,12.bmp like that how i can?

 Accepted Answer

Get all files structure to one variable
cd <your directory>
d = dir('**/*.bmp')
Apply loop to get every file to structure
filename_full = fullfile(d(loop variable).folder, d(loop variable).name);
[~, filename, ext] = fileparts(filename_full);
check file names use ifelse condition
if strcmp(filename, '1');
filename = '9' % so to all other 3 files
end
concatanate all string to form renamed path
filename_full_ren = fullfile(d(loop variable).folder,[filename, ext]);
then rename file by moving
movefile(filename_full, filename_full_ren);
You are done

5 Comments

Error using movefile
Cannot copy or move a file or directory onto itself.
Error in renamefile (line 16)
movefile(filename_full, filename_full_ren);
cd db
d = dir('**/*.bmp');
for i = 1 : 10 % no of classes 100
for j = 1 : 4
filename_full = fullfile(d(j).folder, d(j).name);
[~, filename, ext] = fileparts(filename_full);
count=9;
if strcmp(filename,filename_full)
filename = count; % so to all other 3 files
end
filename_full_ren = fullfile(d(j).folder,[filename, ext]);
movefile(filename_full, filename_full_ren);
count=count+1;
end
end
cd db
d = dir('**/*.bmp');
for ii = 1:length(d)
filename_full = fullfile(d(ii).folder, d(ii).name);
[~, filename, ext] = fileparts(filename_full);
if strcmp(filename,'1')
filename = '9';
elseif strcmp(filename,'2')
filename = '10';
elseif strcmp(filename,'3')
filename = '11';
elseif strcmp(filename,'4')
filename = '12';
end
filename_full_ren = fullfile(d(ii).folder,[filename, ext]);
movefile(filename_full, filename_full_ren);
end
it works but changes are in only first folder
cd testn;
d = dir('**/*.bmp');
for j=1:10
for ii = 1:4
filename_full = fullfile(d(ii).folder, d(ii).name);
[~, filename, ext] = fileparts(filename_full);
if strcmp(filename,'1')
filename = '9';
elseif strcmp(filename,'2')
filename = '10';
elseif strcmp(filename,'3')
filename = '11';
elseif strcmp(filename,'4')
filename = '12';
end
filename_full_ren = fullfile(d(ii).folder,[filename, ext]);
movefile(filename_full, filename_full_ren);
end
end
for ii = 1:4 % you have written for 4 files i think change for loop count as
end
for ii = 1:length(d) % change to this
yes it works, thanks
cd testn;
d = dir('**/*.bmp');
for ii = 1:length(d) % change to this
filename_full = fullfile(d(ii).folder, d(ii).name);
[~, filename, ext] = fileparts(filename_full);
if strcmp(filename,'1')
filename = '9';
elseif strcmp(filename,'2')
filename = '10';
elseif strcmp(filename,'3')
filename = '11';
elseif strcmp(filename,'4')
filename = '12';
end
filename_full_ren = fullfile(d(ii).folder,[filename, ext]);
movefile(filename_full, filename_full_ren);
end

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!