Create dummy files based on filename

Hello,
I have a directory with text files. They are named as A1,A2,A3,A4,A5, B1,B2,B3,B4,B5,C1,C2... and so on. Set A is one group of txt files, Set B is another and so on. All the sets should have (let's say) 5 txt files. However, not all the sets satisfy this requirement.
For example, I have sets like, D1, D3, D4, E5, F1, F2, F5.. etc. (note that Set D is missing D2, D5 and Set E is missing E1, E2, E3, E4 and so on)
Is it possible to read the directory, identify the missing files and create dummy/empty txt files matching the names as requirement specifies?
Any help/lead is warmly appreciated.
TIA!

 採用された回答

Walter Roberson
Walter Roberson 2020 年 3 月 19 日

1 投票

suffix = '.txt'; %or as appropriate
prefixes = 'A': 'F';
for P = prefixes
for K = '1' : '5'
thisfile = [P K suffix];
if ~exist(thisfile, 'file')
fid = fopen(thisfile, 'w');
fclose(fid);
end
end
end

3 件のコメント

Jake
Jake 2020 年 3 月 19 日
Thank you. This works fine!
Now, this is a side question just for my curiosity.
K = '1' : '5'
deals with consecutive numbers. (A1 to A5) What if I want to have file names with non-consecutive numbers, without any pattern? (like, A1, A3, A4, A5 or something like that)
Is it possible to specify a set of numbers to K?
Say, my file set is A1, A2, A5, A9, A13, A56. And my second file set is B1, B2, B56 (Note that B5, B9, B13 are missing) and I want to create the missing files.
I mean, can I still use : when I don't have a pattern or a limit? I hope my question makes sense :)
Walter Roberson
Walter Roberson 2020 年 3 月 19 日
suffix = '.txt'; %or as appropriate
prefixes = 'A': 'F';
for P = prefixes
for K = {'1', '2', '5', '9', '13', '56'}
thisfile = [P K{1} suffix];
if ~exist(thisfile, 'file')
fid = fopen(thisfile, 'w');
fclose(fid);
end
end
end
Jake
Jake 2020 年 3 月 19 日
Thank you, Walter!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

質問済み:

2020 年 3 月 19 日

コメント済み:

2020 年 3 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by