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
嘗試currentFolderDir = PWD – nayab
正如評論中,試圖,而且它也不能工作。
在第二級的深度顯示'0',這不是正確的行爲。 –您不要將目錄添加到文件中。你應該使用'fullfile(currentFile,entries(i).name)' - 否則matlab無法找到目錄。 – bdecaf