How can I rename images in a folder

38 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2019-5-23
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?
Ronny Rives
Ronny Rives 2019-5-23
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.

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2019-5-23
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 个评论
Ronny Rives
Ronny Rives 2019-5-24
编辑:Ronny Rives 2019-5-24
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?
Geoff Hayes
Geoff Hayes 2019-5-24
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.

请先登录,再进行评论。

更多回答(0 个)

类别

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