Changing specific characters in filenames across subfolders
Mostrar comentarios más antiguos
How can I change the first character "r" by another character "f" in all the filnames from multiple subfolders:
Directory folder1/folder2/folder3/
then, multiple files starting with "r" are located in multiple folders. Eg., (S001, S002) :
S001
rXXX11.nii
rXXX32.nii
S002
rXXX41.nii
rXXX63.nii
One unsuccesfull trial:
SourcePath ='D:\folder1\folder2\folder3\';
Participant = 'S';
index = 1001 : 1602;%Index the subjs according to their folder order (e.g., from 001 to 0602)
for i = 1 : length(index)
newParticipant=num2str(index(i));
newParticipant(1)='S';
%To setting the path's source folder iterating the subject folder
SourceFolder = fullfile([SourcePath,newParticipant]);
str=SourceFolder;
pat ='r*.nii';
newStr = replace(str,pat,'f*.nii');
end
Many thanks for any input.
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 19 de Dic. de 2022
SourcePath ='D:\folder1\folder2\folder3';
dinfo = dir(fullfile(SourcePath', 'S*', 'r*.nii'));
filenames = fullfile( {dinfo.folder}, {dinfo.name} );
newfilenames = regexprep(filenames, '\\r(\w+\.nii)', '\\f$1');
arrayfun(@(OLD,NEW) movefile(OLD, NEW), filenames, newfilename, 'uniform', 0);
1 comentario
julian gaviria
el 20 de Dic. de 2022
Movida: Voss
el 28 de Dic. de 2022
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!