How do I search for a wildcard file pattern (\**\* or /**/*) with in a directory in an OS independent way

109 ビュー (過去 30 日間)
Kannan U V
Kannan U V 2018 年 5 月 11 日
回答済み: Walter Roberson 2018 年 5 月 11 日
I need to search recusrsively for all the files in directory with a pattern p05 within the directory homeDirectory
I can use the following in windows elastixInputFiles = dir ([homeDirectory '\**\*p05*']);
and in mac/linux elastixInputFiles = dir ([homeDirectory '/**/*p05*']);
Is there any other cleaner way?

回答 (3 件)

Ameer Hamza
Ameer Hamza 2018 年 5 月 11 日
See fullfile(). It will create a complete path in an OS independent way. In your case,
elastixInputFiles = dir (fullfile(homeDirectory, '**', '*p05*'));
Also refer to this FEX submission for more details: https://www.mathworks.com/matlabcentral/fileexchange/1492-subdir--new-.

Star Strider
Star Strider 2018 年 5 月 11 日
Use the *filesep (link) function to get the file separator for the system you are running your code on:
f = filesep;
elastixInputFiles = dir(sprintf('%s%c**%c*p05',homeDirectory,f,f));
Experiment to get the result you want.

Walter Roberson
Walter Roberson 2018 年 5 月 11 日
You can always use / as a directory specifier on MS Windows. / is the actual internal directory specifier; \ was used for the shell level because / was already in use for command line switches at the shell level at the time that directories were designed into MS Windows.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by