How to Rename Files in Multiple Folders with the Same Names

조회 수: 2 (최근 30일)
Jack Morgan
Jack Morgan 2020년 3월 20일
댓글: Jack Morgan 2020년 3월 20일
I have several folders called Cn or Fn where n is a number, which each contain the same files and I would like to rename some of these in each folder.
An example of the files currently is in the screenshot below. As one example of what I want to achieve I would like to change the name of the file 'report-file-1.out' to 'b1-moment-z-rfile.out' across all of my folders.
Is anyone able to help me with this?
Thanks in advance.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 20일
Try something like this
files = dir('**/report-file-1.out'); % <--- enter current name
new_name = 'b1-moment-z-rfile.out'; % <- enter new name
for i=1:numel(files)
current_path = fullfile(files(i).folder, files(i).name);
new_path = fullfile(files(i).folder, new_name);
movefile(current_path, new_path);
end
  댓글 수: 3
Ameer Hamza
Ameer Hamza 2020년 3월 20일
It will change all the files named report-file-1.out in the current folder and all its sub-folders. It will not go above the current folder.
Jack Morgan
Jack Morgan 2020년 3월 20일
Works exactly like I wanted. Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by