Rename files: keep only first 5 characters
Show older comments
I have a folder with 250 pictures which I want to rename. The original names are "00XXX.[random numbers]" (with XXX from 001 to 250) and I would like them to simply be named "00XXX", deleting the remaining characters.
Is there an easy way of doing this in a for loop?
Thanks in advance!
Accepted Answer
More Answers (1)
Folder = 'C:\Your\Folder';
List = dir(fullfile(Folder, '*.*'));
List([List.isdir]) = []; % [EDITED] remove folders
for k = 1:numel(List)
Name = List(k).name;
Source = fullfile(Folder, Name);
Dest = fullfile(Folder, strtok(Name, '.'));
[Succ, Msg] = movefile(Source, Dest, 'f');
if Succ == 0
error(Msg);
end
end
2 Comments
ABCDEFG HIJKLMN
on 14 Mar 2022
Edited: ABCDEFG HIJKLMN
on 14 Mar 2022
Jan
on 14 Mar 2022
Then some of the files in this folder do not match the naming scheme "00XXX.[random numbers]". Maybe "." and ".." are the problem. I've updated the code, but if the other answer is working, stay at it.
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!