How can I rename images in a folder

30 Ansichten (letzte 30 Tage)
Ronny Rives
Ronny Rives am 23 Mai 2019
Kommentiert: Geoff Hayes am 24 Mai 2019
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 Kommentare
Geoff Hayes
Geoff Hayes am 23 Mai 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?
Ronny Rives
Ronny Rives am 23 Mai 2019
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 23 Mai 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 Kommentare
Ronny Rives
Ronny Rives am 24 Mai 2019
Bearbeitet: Ronny Rives am 24 Mai 2019
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 am 24 Mai 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by