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

조회 수: 1 (최근 30일)
alpedhuez
alpedhuez 2022년 6월 25일
댓글: alpedhuez 2022년 6월 28일
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?

채택된 답변

Image Analyst
Image Analyst 2022년 6월 25일
편집: Image Analyst 2022년 6월 25일
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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by