rename files inside the folder

조회 수: 1 (최근 30일)
Turbulence Analysis
Turbulence Analysis 2022년 3월 23일
댓글: Turbulence Analysis 2022년 3월 23일
I have to rename files that is placed inside the folder, for e.g. in the below shown example 080 needs to be replaced as 020.. Any help ??
1_ss_ddd_080_y000.txt
1_ss_ddd_080_y001.txt
1_ss_ddd_080_y002.txt

채택된 답변

Voss
Voss 2022년 3월 23일
편집: Voss 2022년 3월 23일
old_file_names = { ...
'1_ss_ddd_080_y000.txt', ...
'1_ss_ddd_080_y001.txt', ...
'1_ss_ddd_080_y002.txt'};
new_file_names = strrep(old_file_names,'080','020');
for ii = 1:numel(old_file_names)
movefile(old_file_names{ii},new_file_names{ii});
end
  댓글 수: 5
Voss
Voss 2022년 3월 23일
편집: Voss 2022년 3월 23일
I guess one of the files didn't have '080' in the name (possibly because it was already renamed by a previous run of this code).
To handle that situation, I skip the file in that case:
your_directory = 'C:\path\to\some\folder';
s = dir(fullfile(your_directory,'*.txt'));
old_file_names = fullfile(your_directory,{s.name});
new_file_names = fullfile(your_directory,strrep({s.name},'080','020'));
for ii = 1:numel(old_file_names)
if strcmp(old_file_names{ii},new_file_names{ii})
continue
end
movefile(old_file_names{ii},new_file_names{ii});
end
Turbulence Analysis
Turbulence Analysis 2022년 3월 23일
It's perfect now... Thanks a lot..

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by