How to rename files in subfolders within a folder

6 views (last 30 days)
Deep
Deep on 19 Oct 2018
Edited: Deep on 22 Oct 2018
I have multiple files in subfolders within a main folder. I want to rename the files by inserting some text before the actual file name.
For example I have subfolders with different names 32_post, 43, 44 etc. and each subfolder has different files smooth_frame0.nii.gz, moved_frame.nii.gz and I want to rename these files as P32_post_smooth_frame0.nii.gz, and so on...
  2 Comments
Deep
Deep on 22 Oct 2018
Edited: Deep on 22 Oct 2018
Thanks for your help. However, I have a follow up question. I have the old file name (eg. smooth_frame0.nii.gz) and I have the folder name as a string (eg. 12_post). How do I create a new file name 12_post_smooth_frame0.nii.gz ?
D = dir;
D = D(~ismember({D.name}, {'.', '..'}));
for k = 1:length(D)
currD = D(k).name;
fList = dir(currD); % Get the file list in the subdirectory
fList = fList(~ismember({fList.name}, {'.', '..'}));
oldFileName = fList(k).name;
newFileName = ???????
movefile(oldFileName, newFileName);
end

Sign in to comment.

Answers (0)

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!