-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg_ls.m
executable file
·201 lines (182 loc) · 6.96 KB
/
g_ls.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
function [ FolderFileList ] = g_ls(input)
%
%__________________________________________________________________________
% SUMMARY OF G_LS
%
% List the folders or the files under the path user inputs according to
% users' needs
%
% SYNTAX:
%
% 1) g_ls( input )
%__________________________________________________________________________
% INPUTS:
%
% INPUT
% (string)
% The full path of the NIfTI data.
%__________________________________________________________________________
% OUTPUTS:
%
% Cell of strings, each of which is the full path of folder or the file
% under the folder user inputs.
%__________________________________________________________________________
% COMMENTS:
%
% Copyright (c) Zaixu Cui, Gaolang Gong, State Key Laboratory of Cognitive
% Neuroscience and Learning, Beijing Normal University, 2012.
% Maintainer: [email protected]
% See licensing information in the code
% keywords: dir
% Please report bugs or requests to:
% Zaixu Cui: <a href="[email protected]">[email protected]</a>
% Suyu Zhong: <a href="[email protected]">[email protected]</a>
% Gaolang Gong (PI): <a href="[email protected]">[email protected]</a>
% Permission is hereby granted, free of charge, to any person obtaining a
% copy of this software and associated documation files, to deal in the
% Software without restriction, including without limitation the rights to
% use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included
% in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
% OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
% NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
% DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
% OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
% USE OR OTHER DEALINGS IN THE SOFTWARE.
% Replace string '//', '///', ... with '/'
tmp_ls = input;
input = '';
input(1) = tmp_ls(1);
m = 1;
for i = 2:length(tmp_ls)
if tmp_ls(i) == filesep & tmp_ls(i - 1) == filesep
continue;
else
m = m + 1;
input(m) = tmp_ls(i);
end
end
% Separate the path
SeprateLocations = find(input == filesep);
SeprateQuantity = length(SeprateLocations);
for i = 1:SeprateQuantity - 1
PathUnits{i} = input(SeprateLocations(i) + 1:SeprateLocations(i + 1) - 1);
end
if input(end) ~= filesep
PathUnits{SeprateQuantity} = input(SeprateLocations(SeprateQuantity) + 1:end);
end
% Initialize the Result folders & files List
FolderFileList{1} = filesep;
% Get all the folders & files user needs
for i = 1:length(PathUnits)
% Seek all the index of '*'
StarSymbolIndex = find(PathUnits{i} == '*');
StarSymbolQuantity = length(StarSymbolIndex);
if ~StarSymbolQuantity
for j = 1:length(FolderFileList)
if exist(FolderFileList{j}, 'dir')
if FolderFileList{j} == filesep
FolderFileList{j} = [FolderFileList{j} PathUnits{i}];
else
FolderFileList{j} = [FolderFileList{j} filesep PathUnits{i}];
end
else
FolderFileList{j} = '';
end
end
else
WholeSubDirFile = [];
Quantity = 0;
for j = 1:length(FolderFileList)
% Search all the subfolders and subfiles that meet the rule of PathUnits{i}
% Seek all the subfolders and subfiles under the folders of FoldersList
SubDirFile = [];
if isdir(FolderFileList{j})
SubDirFile = dir(FolderFileList{j});
SubDirFile = {SubDirFile.name};
end
% Judge whether the folder & file in WholeSubDirFile meet the rule of PathUnits{i}
for m = 1:length(SubDirFile)
if ~StarSymbolQuantity
% No star symbol
if ~strcmp(SubDirFile{m}, PathUnits{i})
SubDirFile{m} = '';
end
else
if PathUnits{i}(1) ~= '*'
Prefix = PathUnits{i}(1:StarSymbolIndex(1) - 1);
if length(SubDirFile{m}) >= length(Prefix)
if ~strcmp(Prefix, SubDirFile{m}(1:length(Prefix)))
SubDirFile{m} = '';
end
else
SubDirFile{m} = '';
end
end
if PathUnits{i}(end) ~= '*'
Suffix = PathUnits{i}(StarSymbolIndex(end) + 1:end);
if length(SubDirFile{m}) >= length(Suffix)
if ~strcmp(Suffix, SubDirFile{m}(end - length(Suffix) + 1:end))
SubDirFile{m} = '';
end
else
SubDirFile{m} = '';
end
end
for t = 1:StarSymbolQuantity - 1
Unit = PathUnits{i}(StarSymbolIndex(t) + 1:StarSymbolIndex(t + 1) - 1);
if isempty(findstr(SubDirFile{m}, Unit))
SubDirFile{m} = '';
end
end
end
if ~isempty(SubDirFile{m})
if ~strcmp(SubDirFile{m}, '.') & ~strcmp(SubDirFile{m}, '..')
Quantity = Quantity + 1;
if strcmp(FolderFileList{j}, filesep)
SubDirFile{m} = [FolderFileList{j} SubDirFile{m}];
WholeSubDirFile{Quantity} = SubDirFile{m};
else
SubDirFile{m} = [FolderFileList{j} filesep SubDirFile{m}];
WholeSubDirFile{Quantity} = SubDirFile{m};
end
end
end
end
end
FolderFileList = WholeSubDirFile;
end
end
for i = 1:length(FolderFileList)
if ~exist(FolderFileList{i}, 'file')
FolderFileList{i} = '';
end
end
tmp_ls = FolderFileList;
FolderFileList = '';
j = 0;
for i = 1:length(tmp_ls)
[a,Name,~]=fileparts(tmp_ls{i}); %%xixi
if ~isempty(Name) %%xixi~isempty(tmp_ls{i})
j = j + 1;
FolderFileList{j} = tmp_ls{i};
end
end
if input(end) == filesep
tmp_ls = FolderFileList;
FolderFileList = '';
j = 0;
for i = 1:length(tmp_ls)
if isdir(tmp_ls{i})
j = j + 1;
FolderFileList{j} = tmp_ls{i};
end
end
end
FolderFileList = reshape(FolderFileList, length(FolderFileList), 1);