Run a script on all files in a folder/subfolders

조회 수: 30 (최근 30일)
Steve K
Steve K 2023년 3월 27일
댓글: Steve K 2023년 3월 28일
I presently have a script which uses uigetfile and multiselect to grab several files in a folder and then loops through them; writing them to a table, getting averages of some variables, and then writting those averages to another table with the original filename as a column. The resultant table is then written to a csv after the loop completes. However, I have many folders with this particular type of file and they are separated by a few different parameters in subfolders (month, date, event name, etc.).
Is there a function which I can use to search a particular directory and all subfolders for files with names that meet my criteria? There is a common string in the filename which I can search by, ex. *filetype*.csv. I have used the dir command to return all files that meet the criteria to the command window but I cannot save them to an array with their respective path in column one and the filename in column two. Whenever you try to assign the dir output to a variable it uses the third option of the dir command to extract file attributes rather than names. I may be missing a function which does what I want already but I haven't found it.
example folder structure:
myfolder_2022
jan
event1
xxxxx_filetype_xxxx.csv
feb
mar
event2
xxxxx_filetype_xxxx.csv
aug
event3
xxxxx_filetype_xxxx.csv

채택된 답변

Fangjun Jiang
Fangjun Jiang 2023년 3월 27일
  댓글 수: 1
Steve K
Steve K 2023년 3월 28일
Yes, this ultimately worked for me after some tweaks and other searches Can you use DIR to list files in subfolders ? - MATLAB Answers - MATLAB Central (mathworks.com)
Thank you
x_path=uigetfile('X/XY/')
file_info=dir(fullfile(x_path,'**','*file_string*.csv'));
filenames=fullfile({file_info.folder},{file_info.name});
I was then able to iterate through the filenames with my script.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

chrisw23
chrisw23 2023년 3월 28일
..some example lines of code using .net System functionality
import System.IO.*
searchDir = "C:\Temp";
pattern = "*.*";
searchOption = System.IO.SearchOption.AllDirectories;
fsEnumerableIterator = Directory.EnumerateFiles(searchDir, pattern, searchOption);
enFilesFound = fsEnumerableIterator.GetEnumerator;
while enFilesFound.MoveNext % set up data collector
fileFound = enFilesFound.Current;
dInfo = DirectoryInfo(fileFound);
dInfo.FullName % access DirectoryInfo struct
fInfo = FileInfo(fileFound);
fInfo.FullName % access FileInfo struct
end

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by