3 views (last 30 days)
Show older comments
RJSB on 25 Sep 2022
Commented: RJSB on 26 Sep 2022
Accepted Answer: Turlough Hughes
Open in MATLAB Online
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 Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Turlough Hughes on 25 Sep 2022
Open in MATLAB Online
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
RJSB on 26 Sep 2022
Direct link to this comment
https://ms-www.mathworks.com/matlabcentral/answers/1811950-how-to-use-dir-function-with-customized-number-of-folders#comment_2382835
Worked perfectly, thanks
Sign in to comment.
More Answers (1)
dpb on 25 Sep 2022
Edited: dpb on 25 Sep 2022
Open in MATLAB Online
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
RJSB on 26 Sep 2022
Direct link to this comment
https://ms-www.mathworks.com/matlabcentral/answers/1811950-how-to-use-dir-function-with-customized-number-of-folders#comment_2382840
Thanks for the detailed explanation
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABProgrammingFiles and FoldersFile Operations
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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office