フィルターのクリア

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

93 ビュー (過去 30 日間)
George
George 2022 年 7 月 6 日
回答済み: Stephen23 2022 年 7 月 6 日
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 件のコメント
dpb
dpb 2022 年 7 月 6 日
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...

サインインしてコメントする。

回答 (2 件)

Benjamin Thompson
Benjamin Thompson 2022 年 7 月 6 日
The dir function will work with wildcard characters:
D = dir(fullfile(myFolder, '*grab*'))

Stephen23
Stephen23 2022 年 7 月 6 日
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.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by