Effacer les filtres
Effacer les filtres

How do I grab all the files with a certain name pattern from several folders?

93 vues (au cours des 30 derniers jours)
George
George le 6 Juil 2022
Hi, I have text files with the same name format that I want to grab from several folders (also with the same name format) all at once (to then compile the text files into one file).
I read on another post that below is the format to grab it but I cannot get it to work:
filePattern = fullfile(myFolder, '**Folder/*')
(to display what I am looking for, I want to grab all of the files with "grab" in them)
All_files -> Folder1 -> no1
-> grab1
-> Folder2 -> no2
-> grab2
-> Folder3 -> no3
-> grab3
Thank you!
  1 commentaire
dpb
dpb le 6 Juil 2022
See the example dir "Find Files in Subfolders" that should work ok for your case that is pretty simple pattern.
I find it easier to often dispatch a dir command to the OS where can use the OS switches and more expanded wildcard matching than what the MATLAB dir() implements, particularly with the JPSoftware command replacement that is far more powerful than the MS-supplied tools if one is on Winders...

Connectez-vous pour commenter.

Réponses (2)

Benjamin Thompson
Benjamin Thompson le 6 Juil 2022
The dir function will work with wildcard characters:
D = dir(fullfile(myFolder, '*grab*'))

Stephen23
Stephen23 le 6 Juil 2022
All_files -> Folder1 -> no1
-> grab1
-> Folder2 -> no2
-> grab2
-> Folder3 -> no3
-> grab3
The double asterisk is a wild wildcard which recursively matches any folder names. It is clearly specified in the DIR documentation that "Characters next to a ** wildcard must be file separators", which your code does not do.
Here are two approaches:
P = 'absolute or relative path to All_Files';
S = dir(fullfile(P,'**','grab*')); % recursively match all subfolders
S = dir(fullfile(P,'Folder*','grab*')) % match subfolders named "Folder..."
Note that you should specify the file extension too.

Catégories

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

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by