MATLAB Answers

0

How can I rename images in a folder

Asked by Ronny Rives on 23 May 2019 at 16:13
Latest activity Commented on by Geoff Hayes
on 24 May 2019 at 11:17
Hello all,
I have several images named as:
Basler piA1000-60gm (22085941)_20190522_213447520_0001.bmp
Basler piA1000-60gm (22085941)_20190522_213447520_0002.bmp
...
I'd like to rename the images and keep the timestamp as:
20190522_213447520_0001.bmp
20190522_213447520_0002.bmp
or, if it is possible, I'd prefer rename the images with the timestamp when the images were created, for example using this format: 2019-05-22-21-34-47.520.
Thank you.

  2 Comments

Ronny - are all files prefixed with "Basler piA1000-60gm (22085941)_" and it is that string that you want to use? Or do some file names have different prefixes?
Hi Geoff, all files are prefixed with "Basler piA1000-60gm (22085941)_" and I want to remove this string from all images.
At the same time, I want to keep the other string: 20190522_213447520_0001 (that is basically the timestamp (20190522_213447520).
Thank you for your help.

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 23 May 2019 at 17:07
 Accepted Answer

Ronny - let's assume that you have a list of file names (with or without a full path to the file). We can use strrep and movefile to replace the file name (remove the "prefix") and move the file respectively. For example,
for k = 1:length(myFilesToMove)
fromFilename = myFilesToMove{k};
toFilename = strrep(fromFilename, 'Basler piA1000-60gm (22085941)_', '');
movefile(fromFilename, toFilename);
end
where myFilesToMove is a cell array of your different files that you want to rename.

  2 Comments

Geoff, your answer solved the problem of removing the prefix from image names. Now I am trying to rename images according to the date of their creation. I have this code,
projectdir = 'E:\2019\20190522\Images';
images = dir( fullfile(projectdir, '*.bmp') );
oldnames = {images.name};
dates = {images.date};
newnames = strrep(oldnames, 'oldnames', 'dates');
for K = 1 : length(oldnames)
movefile( fullfile(projectdir, oldnames{K}), fullfile(projectdir, newnames{K}));
end
but it doesn't work. How can I solve this problem?
Ronny - I think that all you need to do is just change each file name as you iterate over it (rather than trying to change them all at once)
projectdir = 'E:\2019\20190522\Images';
images = dir( fullfile(projectdir, '*.bmp') );
for K = 1 : length(oldnames)
oldFilename = images(K).name;
newFilename = strrep([images(K).date '.bmp'), ' ', '_'];
movefile( fullfile(projectdir, oldFilename), fullfile(projectdir, newFilename));
end
In the above, the code replaces all space characters (that might appear in the new file name) with underscores.
Note a problem with this line
newnames = strrep(oldnames, 'oldnames', 'dates');
is that oldnames is an array (cell?) and you are trying to replace the string called 'oldnames' with the string called 'dates'. Even if you do remove the apostrophes, I'm not sure of the behaviour.

Sign in to comment.