How can I rename images in a folder
31 views (last 30 days)
Show older comments
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
Geoff Hayes
on 23 May 2019
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?
Accepted Answer
Geoff Hayes
on 23 May 2019
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 Hayes
on 24 May 2019
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.
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!