The directory recursive traversal does not go beyond 2 levels.
Why is this so?
=============================================================
currentFolderDir = '.'; % pwd
% path('C:\Users\EI\Documents\MATLAB\OO\Simple Object Creation in Object Oriented');
depthLevel = 0;
folderCount = 0;
fileCount = 0;
fprintf('=====================================\n');
fprintf('Depth level: %d\n', depthLevel);
[folderCount, fileCount] = fileDirectoryRecursiveTraversal (currentFolderDir, depthLevel, folderCount, fileCount);
=============================================================
function [folderCount, fileCount] = fileDirectoryRecursiveTraversal (currentFile, depthLevel, folderCount, fileCount)
for i = 1:depthLevel
fprintf('\t\t');
end
fprintf('%s\n', currentFile ); % Print the name of the entry
%isdir(currentFile) % [ERR] can't go beyond level 2
%pause;
if (isdir(currentFile)) % Check if it is a directory
folderCount = folderCount + 1;
fprintf('\nThere are %d folders.\n', folderCount);
pause
depthLevel = depthLevel + 1;
fprintf('=====================================\n');
fprintf('Depth level: %d\n', depthLevel);
% Get a list of all the entries in the directory
entries = dir(currentFile);
% entries(1).name = '.'
% entries(2).name = '..'
numberOfEntries = length(entries); % including current folder and pointer to folder 1 level up
% Ensure that the list is not null
% if( (numberOfEntries - 2) ~= 0 ) % 2: % entries(1).name = '.'; % entries(2).name = '..'
if(numberOfEntries ~= 2)
% Loop over all the entries
for i = 3:numberOfEntries
% Recursive call to traverse
[folderCount, fileCount] = fileDirectoryRecursiveTraversal( entries(i).name, depthLevel, folderCount, fileCount); % i = 3:numberOfEntries
end
fprintf('\nDepth level: %d\n', depthLevel);
fprintf('There are %d files.\n\n', fileCount);
fileCount = 0;
else
% disp('cccccccccccccccccccc')
fileCount = 0; % empty folder
end
else
fileCount = fileCount + 1;
folderCount = 0;
end
folderCount = folderCount - 1;
depthLevel = depthLevel - 1; % exit level
end
I have adapted a function I use to recursively process files in certain directory. It correctly goes through all the subdirectories and shows the filenames, but it doesn’t show the depthlevel, folderCount and fileCount. It should be easy to adapt, but if you need help just let me know :