Azzera filtri
Azzera filtri

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

93 visualizzazioni (ultimi 30 giorni)
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 Commento
dpb
dpb il 6 Lug 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...

Accedi per commentare.

Risposte (2)

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

Stephen23
Stephen23 il 6 Lug 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.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by