-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetDirAll.m
55 lines (50 loc) · 1.99 KB
/
GetDirAll.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function [images, folders] = GetDirAll(searchDir, isWindows)
%GETDIRALL List contents of a directory and its subdirectories.
% [images, folders] = GETDIRALL(searchDir, isWindows) returns two cell
% arrays: 'folders' containing the paths of all subdirectories, and
% 'images' containing the paths of all files in these directories.
%
% 'searchDir' is the directory to be searched.
% 'isWindows' is a boolean indicating if the path is for Windows (true)
% or Unix/Linux (false).
% Initialize variables
tempFolders = {searchDir};
folders = {searchDir};
images = {};
folderLayer = 1;
counterDir = 1;
counterFile = 1;
hasMoreFolders = true;
% Main loop to process directories
while hasMoreFolders
tempFoldersNextLayer = {};
counterDirTemp = 1;
hasMoreFolders = false;
for j = 1:length(tempFolders)
contents = dir(tempFolders{j});
for i = 3:length(contents) % Skip '.' and '..' entries
item = contents(i);
fullPath = fullfile(tempFolders{j}, item.name);
if item.isdir
% Add directory to folders list
hasMoreFolders = true;
if isWindows
folders{folderLayer + 1, counterDirTemp} = fullPath;
else
folders{folderLayer, counterDirTemp} = fullPath;
end
tempFoldersNextLayer{counterDirTemp} = fullPath;
counterDirTemp = counterDirTemp + 1;
counterDir = counterDir + 1;
else
% Add file to images list
images{counterFile} = fullPath;
counterFile = counterFile + 1;
end
end
end
% Prepare for next layer of folders
tempFolders = tempFoldersNextLayer;
folderLayer = folderLayer + 1;
end
end