count the # of rows of CSV files in a folder, part 2

3 views (last 30 days)
https://www.mathworks.com/matlabcentral/answers/1748155-count-the-of-rows-of-csv-files-in-a-folder?s_tid=srchtitle discussed how to count the total # of rows from CSV files in a folder. Now I want to consider a situation that there are subfolders in the folder that contains CSV files and I want to count the total # of rows from CSV files in all subfolders.
D = 'full path to the main folder';
S = dir(fullfile(D,'*'));
N = setdiff({S([S.isdir]).name},{'.','..'}); % list of subfolders of D.
numRowsTotal = 0;
for ii = 1:numel(N)
T = dir(fullfile(D,N{ii},'*')); % improve by specifying the file extension.
C = {T(~[T.isdir]).name}; % files in subfolder.
for jj = 1:numel(C)
F = fullfile(D,N{ii},C{jj})
% do whatever with file F.
datatable = readtable(F, 'ReadVariableNames', false); %or true if there is a header
numRowsTotal = numRowsTotal + height(datatable);
end
end
Will this work?

Accepted Answer

Image Analyst
Image Analyst on 25 Jun 2022
Edited: Image Analyst on 25 Jun 2022
Maybe, but that's unnecessarily complicated. Just use ** in dir to get only csv files in the top level folder and subfolders and don't worry about isdir and set diff and two loops
folder = pwd;
fileList = dir(fullfile(folder,'**\*.csv'));
totalNumberOfLines = 0;
for k = 1 : numel(fileList)
fullFileName = fullfile(fileList(k).folder, fileList(k).name);
t = readtable(fullFileName);
theseLines = height(t);
totalNumberOfLines = totalNumberOfLines + theseLines;
fprintf('%s has %d rows.\n', fullFileName, theseLines)
end
fprintf('In %d CSV files there are %d lines total.\n', numel(fileList), totalNumberOfLines)

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!