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

1 Ansicht (letzte 30 Tage)
alpedhuez
alpedhuez am 25 Jun. 2022
Kommentiert: alpedhuez am 28 Jun. 2022
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?

Akzeptierte Antwort

Image Analyst
Image Analyst am 25 Jun. 2022
Bearbeitet: Image Analyst am 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)

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by