How to rename multiple files in a folder with variable names depending on their names in another folder?
2 ビュー (過去 30 日間)
古いコメントを表示
Abdel-Rahman Ashraf
2020 年 6 月 12 日
コメント済み: Abdel-Rahman Ashraf
2020 年 6 月 12 日
I have several files in a directory (some examples of the names of the files are commented under the commented word (oldnames)), and I want to rename them as the commented ones ( under the commented word (newnames)). So I want to extract every number that has 4 digits (e.g., (3185)) and use it to rename the new files. I tried to extract the numbers that have 4 digits as below but it didn't work. Any suggesstions?
projectdir = 'C:\Users\Abdelrahman\Downloads\Documents\CIV312_Test';
oldfiles = dir( fullfile(projectdir, '*3*.pdf') );
oldnames = {oldfiles.name};
myindices = cellfun(@isempty,regexp(oldnames,'*3\d\d\d*','match')); % didn't work
% oldnames
% (CIV-312-3154)_Template.pdf
% 3140_CIV312.pdf
% CIV _312_3085.pdf
% CIV-312-3051.pdf
% newnames
% CIV_312_3154.pdf
% CIV_312_3140.pdf
% CIV_312_3085.pdf
% CIV_312_3051.pdf
0 件のコメント
採用された回答
Mohammad Sami
2020 年 6 月 12 日
編集済み: Mohammad Sami
2020 年 6 月 12 日
You dont need the *. you can do as follows.
match = regexp(oldnames,'3\d{3}','match');
5 件のコメント
Mohammad Sami
2020 年 6 月 12 日
編集済み: Mohammad Sami
2020 年 6 月 12 日
regexp is for matching, to create a new string, there are various options. One of the option is to use the compose function since you are following a template name.
newnames = compose('CIV_312_%s.pdf',string(match2));
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!