2013-01-12 49 views
0

目錄遞歸遍歷不超過2個級別。 這是爲什麼呢?目錄遞歸遍歷不超過Matlab中的2個級別

 
============================================================= 


    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 
+0

嘗試currentFolderDir = PWD – nayab

+2

正如評論中,試圖,而且它也不能工作。

 entries = dir(currentFile); length(entries) 
在第二級的深度顯示'0',這不是正確的行爲。 –

+1

您不要將目錄添加到文件中。你應該使用'fullfile(currentFile,entries(i).name)' - 否則matlab無法找到目錄。 – bdecaf

回答

4

我已經適應了我用來遞歸處理某些目錄中的文件的函數。它正確地遍歷所有子目錄並顯示文件名,但它不顯示depthlevel,folderCount和fileCount。它應該很容易適應,但如果你需要幫助,只是讓我知道:

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

謝謝迪尼亞。 –