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

143 Ansichten (letzte 30 Tage)
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 Kommentar
dpb
dpb am 6 Jul. 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...

Melden Sie sich an, um zu kommentieren.

Antworten (2)

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

Stephen23
Stephen23 am 6 Jul. 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.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by