How do I count the number of .CSV files in a folder?

Hi,
I would like to know how to count the number of .CSV files in my folder? I have to do this for multiple folders so I would like to choose them.
So far I have something like this...
source_dir = uigetdir([]);
d = dir([source_dir, '\*.csv']);
n=length(d(not([d.isdir])));
but I get an error at the second line. Help!

Answers (1)

n=length(d)

6 Comments

It's not just this command because my error occurs at the definition of d
This worked fine for me:
% Look for m files in current folder and count them.
source_dir = pwd; %uigetdir([]);
d = dir([source_dir, '\*.m']);
n=length(d)
No error whatsoever. Changing it to look for csv files will not then cause an error.
this is not working for me, counts zero
Then you don't have any .m files off the source_dir. Check your folder and make sure there are m-files there or else change your file pattern. Make sure you don't have a double backslash in there. You might use full file instead fullfile(source_dir, '*.m').
Do you use Mac OS? I had the same problem but then realised it's because I'm supposed to use '/' instead of '\' because it's not Windows.
because I'm supposed to use '/' instead of '\' because it's not Windows.
... and that is why you use fullfile instead of building your own paths by string concatenation. fullfile will do the right thing regardless of which platform you're on.

Sign in to comment.

Categories

Asked:

on 18 Nov 2013

Commented:

on 3 Jun 2017

Community Treasure Hunt

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

Start Hunting!