Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9129679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:48:14+00:00 2026-06-17T07:48:14+00:00

The directory recursive traversal does not go beyond 2 levels. Why is this so?

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:48:15+00:00Added an answer on June 17, 2026 at 7:48 am

    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 :

    function processDirectory(path)
    
    if ~strcmp(path(end), filesep)
        path(end+1)=filesep;
    end
    dirInfo= dir(path);
    files=~[dirInfo.isdir];
    fileNames={dirInfo(files).name};
    disp(path);
    if ~isempty(fileNames)
        for i=1:length(fileNames)
            % Do whathever (show file names?)
            disp(fileNames(i));
        end
    end
    
    % For each subdir, call itself again
    isDir=[dirInfo.isdir];
    dirNames={dirInfo(isDir).name};
    dirNames(strcmp(dirNames, '.') | strcmp(dirNames, '..'))=[];
    
    for i=1:length(dirNames)
        processDirectory([path dirNames{i} filesep]);    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I have this recursive method which deletes empty folders: private void DeleteEmpty(DirectoryInfo directory) {
In Perl we usually do a recursive directory traversal using File::Find and we often
I'm using this asynchronous recursive function to iterate through a directory's files and folders,
Let's say I want to scan through every file in this directory (recursive, too).
I have been working on a recursive directory scan which scans folders and adds
I am trying to get an HTML-based recursive directory listing based on code here:
I've already build a recursive function to get the directory size of a folder
How can i download all directory from server recursively. I use WinINet and this
I've recently been implementing a recursive directory search implementation and I'm using a Stack

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.