Ana-Maria Tannous
Ana-Maria Tannous

Reputation: 13

How to rename all the images in the directory

I have a directory that contains 200 jpeg images. What I want is to rename all these images. So how can I rename all my images at the same time. for example I want to rename the first image to "hello1", "hello2" for the second, "hello3" for the third....>"hello200" for the 200.

Below you can find my code:

maximagesperdir = inf;
directory='imagess';
dnames = {directory};
fprintf('Reading images...');
cI = cell(1,1);
  c{1} = dir(dnames{1});
  if length(c{1})>0,
    if c{1}(1).name == '.',
      c{1} = c{1}(4:end);
    end
  end
  if length(c{1})>maximagesperdir,
    c{1} = c{1}(1:maximagesperdir);
  end
  cI{1} = cell(length(c{1}),1);
  for j = 1:length(c{1}),
    cI{1}{j} = double(imread([dnames{1} '/' c{1}(j).name]))./255;
  end
fprintf('done.\n');

Upvotes: 0

Views: 669

Answers (2)

dinos66
dinos66

Reputation: 706

If you're just looking to rename the files and not to perform an operation on the images and then rename, there's always the total commander program which is a useful little thing to have. You select all the files and by using ctrl+m you select the way by which you would like to rename them (date, name, etc). Very simple if you're looking to perform the renaming operation scarcely. I'm just saying...

Upvotes: 0

outdoor_guy
outdoor_guy

Reputation: 146

Here is some code to rename all the files in the current directory, the code you showed appears to read and not rename.

fnames = dir('*.jpg');
for i = 1:length(fnames)
   old_name = fnames(i).name;
   new_name = sprintf('hello%d.jpg', i);
   movefile(old_name, new_name)
end

Upvotes: 1

Related Questions