How to use dir function with customized number of folders
7 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Turlough Hughes
on 25 Sep 2022
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
More Answers (1)
dpb
on 25 Sep 2022
Edited: dpb
on 25 Sep 2022
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')
d1=datetime('05-08-2019','InputFormat',"dd-MM-uuuu")
d2=datetime('06-09-2019','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
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
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!