Effacer les filtres
Effacer les filtres

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

66 vues (au cours des 30 derniers jours)
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?

Réponses (3)

Ameer Hamza
Ameer Hamza le 11 Mai 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 le 11 Mai 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 le 11 Mai 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.

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by