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

90 views (last 30 days)
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?

Answers (3)

Ameer Hamza
Ameer Hamza on 11 May 2018
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 on 11 May 2018
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 on 11 May 2018
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.

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!