How to use dir function with customized number of folders

6 ビュー (過去 30 日間)
RJSB
RJSB 2022 年 9 月 25 日
コメント済み: RJSB 2022 年 9 月 26 日
In the matlab code, I have a folder, lets say C:/Username/myfolder. Inside my folder, I have files with the following names: 101_07-09-2019_.txt, 101_04-08-2019_.txt, 101_02-03-2019_.txt and 101_01-06-2019_.txt, etc. where for example: 07-09-2019 denotes dd-mm-yyyy. I am using a dir command such as
p = dir(['C:\Username\myfolder\']);
This command gives the attributes of all the files contained in myfolder in the variable p.
However, I want to select only those files that are in the month and date range which I specify. Lets say I want the files only from 5th August (05-08-2019) to 6th September (06-09-2019).This means that i require the attributes of only 101_07-09-2019_.txt, 101_04-08-2019_.txt inside the variable p instead of all the files in myfolder. Can you please help me with it?

採用された回答

Turlough Hughes
Turlough Hughes 2022 年 9 月 25 日
You can do the following:
p = dir('C:\Username\myfolder');
dateStringFromFilename = regexp({p.name}.','\d{2}-\d{2}-\d{4}','match','once');
fileDatetimes = datetime(dateStringFromFilename,'InputFormat','dd-MM-yyyy');
If the files haven't been modified since the date that they were created, you could also apply the following to get the fileDatetimes, in place of the above approach:
fileDatetimes = datetime([p.datenum],'ConvertFrom','datenum');
Either way, we can now use isbetween() to get files that are within a specific date range as follows:
tStart = datetime(2019,08,01);
tEnd = datetime(2019,09,10);
idx = isbetween(fileDatetimes,tStart,tEnd);
result = p(idx);
The resulting files will be from dates >= tStart and <=tEnd
  1 件のコメント
RJSB
RJSB 2022 年 9 月 26 日
Worked perfectly, thanks

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

その他の回答 (1 件)

dpb
dpb 2022 年 9 月 25 日
編集済み: dpb 2022 年 9 月 25 日
Go ahead and read all the files into the dir() stuct and then do the culling there...
It's easy enough to parse those filename strings to datetime values and then peform logical tests...let's pretend here from the bread crumbs left above...
rootfolder='C:\Username\myfolder\'; % the root folder location of interest
fbase='101'; % the basename of the files of interest -- keep as data, not code
d=dir(fullfile(rootfolder,[fbase '_*.txt'])); % create wildcard matching file pattern, return files...
Now since the above dir command won't work online(I commented it out to get the below results, then came back now and uncommented it as being illustrative of your needed code), let's pretend we got the above list of files; those are going to be in the struct array d under d.name for each...
n={'101_07-09-2019_.txt';'101_04-08-2019_.txt';'101_02-03-2019_.txt';'101_01-06-2019_.txt'}; % the given filenames list
d=cellfun(@(c)struct('name',c,'folder',rootfolder),n);
fdates=datetime(extractBetween({d.name},'_','_').','InputFormat','dd-MM-uuuu')
fdates = 4×1 datetime array
07-Sep-2019 04-Aug-2019 02-Mar-2019 01-Jun-2019
d1=datetime('05-08-2019','InputFormat',"dd-MM-uuuu")
d1 = datetime
05-Aug-2019
d2=datetime('06-09-2019','InputFormat',"dd-MM-uuuu")
d2 = datetime
06-Sep-2019
d=d(isbetween(fdates,d1,d2))
d = 0×1 empty struct array with fields: name folder
for i=1:numel(d)
fn=fullfile(d(i).folder,d(i).name)
% open, read, do whatever with file here...
end
Of course, your dates given above result in a null set because your two requested dates are outside the listed ones -- if were to swap the days for Sept and Aug between wanted and given, then it would work, but the logic above works as wanted; you just missed on picking a set that encompasses the given...
% fixup the file dates to have a couple inside instead
n={'101_06-09-2019_.txt';'101_06-08-2019_.txt';'101_02-03-2019_.txt';'101_01-06-2019_.txt'}; % the given filenames list
d=cellfun(@(c)struct('name',c,'folder',rootfolder),n);
fdates=datetime(extractBetween({d.name},'_','_').','InputFormat','dd-MM-uuuu');
d=d(isbetween(fdates,d1,d2));
for i=1:numel(d)
fn=fullfile(d(i).folder,d(i).name)
% open, read, do whatever with file here...
end
fn = 'C:\Username\myfolder\/101_06-09-2019_.txt'
fn = 'C:\Username\myfolder\/101_06-08-2019_.txt'
  1 件のコメント
RJSB
RJSB 2022 年 9 月 26 日
Thanks for the detailed explanation

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

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by