-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRangTraversal.m
More file actions
55 lines (51 loc) · 1.39 KB
/
RangTraversal.m
File metadata and controls
55 lines (51 loc) · 1.39 KB
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
%% 代码引用:https://blog.csdn.net/guoxiaojie_415/article/details/21317323
% 获取目录所有文件的路径
function [ mFiles ] = RangTraversal( strPath )
%定义两数组,分别保存文件和路径
mFiles = cell(0,0);
mPath = cell(0,0);
mPath{1}=strPath;
[r,c] = size(mPath);
while c ~= 0
strPath = mPath{1};
Files = dir(strPath);
LengthFiles = length(Files);
if LengthFiles == 0
break;
end
mPath(1)=[];
iCount = 1;
while LengthFiles>0
if Files(iCount).isdir==1
if Files(iCount).name ~='.'
filePath = [strPath filesep Files(iCount).name];
[r,c] = size(mPath);
mPath{c+1}= filePath;
end
else
filePath = [strPath filesep Files(iCount).name];
[row,col] = size(mFiles);
mFiles{col+1}=filePath;
end
LengthFiles = LengthFiles-1;
iCount = iCount+1;
end
[r,c] = size(mPath);
end
mFiles = mFiles';
end
%% 示例
% clc
% clear
% close all
%
% %% The directory of your files
% str = 'C:\test\';
%
% %% The use of depth-first walk
% mFiles = [];
% [mFiles, iFilesCount] = DeepTravel(str,mFiles,0)
% mFiles = mFiles';
%
% %% The use of breadth first walk
% mFiles2 = RangTraversal(str)