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 Center および File ExchangeFile Operations についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by