How to use dir function with customized number of folders (2024)

3 views (last 30 days)

Show older comments

RJSB on 25 Sep 2022

  • Link

    Direct link to this question

    https://ms-www.mathworks.com/matlabcentral/answers/1811950-how-to-use-dir-function-with-customized-number-of-folders

  • Link

    Direct link to this question

    https://ms-www.mathworks.com/matlabcentral/answers/1811950-how-to-use-dir-function-with-customized-number-of-folders

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

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Turlough Hughes on 25 Sep 2022

  • Link

    Direct link to this answer

    https://ms-www.mathworks.com/matlabcentral/answers/1811950-how-to-use-dir-function-with-customized-number-of-folders#answer_1060520

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

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

  • Link

    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

  • Link

    Direct link to this answer

    https://ms-www.mathworks.com/matlabcentral/answers/1811950-how-to-use-dir-function-with-customized-number-of-folders#answer_1060535

  • Link

    Direct link to this answer

    https://ms-www.mathworks.com/matlabcentral/answers/1811950-how-to-use-dir-function-with-customized-number-of-folders#answer_1060535

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

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

  • Link

    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

Tags

  • matlab
  • datetime
  • dir

Products

  • MATLAB

Release

R2018b

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.


How to use dir function with customized number of folders (6)

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

How to use dir function with customized number of folders (2024)
Top Articles
College basketball rankings: Arthur Kaluma boosts Texas as Longhorns give SEC most selections in Top 25 And 1
This Season’s Broadcast-TV Finale Episodes, Ranked From Worst to Best
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Things to do in Wichita Falls on weekends 12-15 September
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
What's the Difference Between Halal and Haram Meat & Food?
R/Skinwalker
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Cubilabras
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Juego Friv Poki
Dirt Devil Ud70181 Parts Diagram
Truist Bank Open Saturday
Water Leaks in Your Car When It Rains? Common Causes & Fixes
What’s Closing at Disney World? A Complete Guide
New from Simply So Good - Cherry Apricot Slab Pie
Drys Pharmacy
Ohio State Football Wiki
Find Words Containing Specific Letters | WordFinder®
FirstLight Power to Acquire Leading Canadian Renewable Operator and Developer Hydromega Services Inc. - FirstLight
Webmail.unt.edu
2024-25 ITH Season Preview: USC Trojans
Metro By T Mobile Sign In
Restored Republic December 1 2022
12 30 Pacific Time
Jami Lafay Gofundme
Stellaris Resolution
Wi Dept Of Regulation & Licensing
Pick N Pull Near Me [Locator Map + Guide + FAQ]
Crystal Westbrooks Nipple
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 5791

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.