Rename files after a certain string in the file itself appears

3 次查看(过去 30 天)
Dear MatLab users,
I have a bunch of files in a folder I need to rename. Basically, I want to delete the first 5 or 6 characters of the filename, those before a certain string. However, I do not know when this occurs, so I need to code it using something like strfind, but haven't managed to do it. Below is the code I have so far:
files_csv = dir('*.csv');
for i = 1 : length(files_csv)
baseFileName = files_csv(i).name;
%newFileName = baseFileName(6:end);
copyfile( baseFileName , newFileName )
end
Instead on newFileName = baseFileName(6:end), which only deletes the first 6 characters, I need to retain the characters after the string "PGA" appears.
Thanks

采纳的回答

Jan
Jan 2022-4-27
An explicit example would be useful: old name and new name.
Maybe:
files_csv = dir('*.csv');
for i = 1 : length(files_csv)
baseFileName = files_csv(i).name;
newFileName = extractAfter(baseFileName, "PGA");
copyfile( baseFileName , newFileName )
end
  1 个评论
Gianluca Regina
Gianluca Regina 2022-4-27
Oh, sorry about that. Nevertheless, your adjustment works perfectly, thank you very much! For future reference, I attach the screenshot of the result of your code.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by