How do I rename multiple files located in subfolders within a main folder?
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Deep
 el 22 de Oct. de 2018
  
    
    
    
    
    Comentada: Deep
 el 23 de Oct. de 2018
            The files have to be renamed in a certain order. For eg. if the file name is Fname, the renaming should be P(foldername)_Fname. Below is the code created using help from comments on other answers. However,I am getting an error ' All inputs must be strings, character vectors, or cell arrays of character vectors.' I am not sure where the problem lies. I would appreciate any help with this.
D = dir; 
  D = D(~ismember({D.name}, {'.', '..'}));
  for k = 1:length(D) 
      currD = D(k).name; 
        fList = dir(currD); 
        fList = fList(~ismember({fList.name}, {'.', '..'}));
        for i = 1:length(fList)
        oldFileName = fList(i).name;
        str1 = {'P'};
        str2 = D(k).name;
        str3 = {'_'};
        str4 = oldFileName;
        newFileName = (strcat(str1, str2, str3, str4));
        movefile (fullfile(D,oldFileName, newFileName));
        end
   end
0 comentarios
Respuesta aceptada
  Akira Agata
    
      
 el 23 de Oct. de 2018
        How about the following?
D = struct2table(dir('./**/*'));
D = D(~D.isdir,:);
for kk = 1:height(D)
  [~,folderName] = fileparts(D.folder{kk});
  newFileName = ['P',folderName,'_',D.name{kk}];
  movefile(fullfile(D.folder{kk},D.name{kk}),fullfile(D.folder{kk},newFileName));
end
Más respuestas (0)
Ver también
Categorías
				Más información sobre File Operations en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!