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
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?
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!