rename files in a folder

9 vues (au cours des 30 derniers jours)
Franziska Motka
Franziska Motka le 22 Fév 2023
Commenté : Franziska Motka le 24 Fév 2023
Hi everyone,
I have a folder with hundreds of files in it, named like CBM32002.MR.STUDIES_PSYCHOLOGY.0001.0001.2023. CBM320 is the participants ID. I accidently named this particants wrong, it would be CBM330 instead of CBM320. Could somebody help me to write a code which is renaming all files in the folder from CBM320... to CBM330... ?
Thanks for any help!
  2 commentaires
Stephen23 le 22 Fév 2023
What is the file extension of the files?
Franziska Motka
Franziska Motka le 22 Fév 2023
they are all IMA files, so e.g. CBM32002.MR.STUDIES_PSYCHOLOGY.0001.0001.2023.

Connectez-vous pour commenter.

Réponse acceptée

Akira Agata
Akira Agata le 22 Fév 2023
How about the following solution?
% Create list of all files in the current folder
s = dir(pwd);
s = struct2table(s);
% Extract the files with the file name of CBM320~
idx = startsWith(, 'CBM320');
s = s(idx,:);
% Create current file names and new file names
oldFileName =;
newFileName = replace(oldFileName, 'CBM320', 'CBM330');
% Change the file name one-by-one
for kk = 1:numel(oldFileName)
movefile(oldFileName{kk}, newFileName{kk});
  1 commentaire
Franziska Motka
Franziska Motka le 24 Fév 2023
Finally, I was able to try the code on my PC. It worked! Thank you very much for your help :)

Connectez-vous pour commenter.

Plus de réponses (0)


En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange



Community Treasure Hunt

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

Start Hunting!

Translated by