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

3 vues (au cours des 30 derniers jours)
alpedhuez
alpedhuez le 25 Juin 2022
Commenté : alpedhuez le 28 Juin 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?

Réponse acceptée

Image Analyst
Image Analyst le 25 Juin 2022
Modifié(e) : Image Analyst le 25 Juin 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)

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by