comparison toolboxes/bioakustik_tools/filesys/forAllFilesInDir.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function y = forAllFilesInDir(startdir, filetypes, func, recursive, y, varargin)
2 % y = forAllFilesInDir(startdir, filetypes, func, recursive, y, varargin)
3 %
4 % Traverse directory structure, performing for each file of a specified filetype
5 % an action defined by a function handle.
6 %
7 % INPUT:
8 % ------
9 % startdir ..... (string) Start directory.
10 % Any directory containing a file named "skip" will be skipped.
11 % filetypes .... (cell array of strings) File type filter, e.g. {'*.amc' '*.asf'}
12 % or {'*.*'} for all files
13 % func ......... (function handle) Worker function to be called for each file
14 % The function designated by func will be passed the current complete
15 % filename (including pathname) as the first argument. If the parameter list
16 % varargin is nonempty, a single cell array with the current filename as the
17 % first entry followed by the remaining parameters in varargin will be passed.
18 % recursive .... (boolean) Flag indicating whether recursive traversal is desired or not.
19 % y ............ (matrix or cell array) Initial value for output container. Usually empty ([]).
20 % varargin ..... (cell array of parameters) Parameters to be passed to func, see "func".
21 %
22 % OUTPUT:
23 % -------
24 % y ............ (matrix or cell array) Collected outputs of calls to func, vertically concatenated.
25 %
26 % EXAMPLE:
27 % --------
28 % y = forAllFilesInDir('S:\data_MoCap\MoCaDaDB\AMC',{'*.amc'},@getAMCLengthInSeconds,true,[]);
29 % y = forAllFilesInDir('S:\data_MoCap\MoCaDaDB\AMC',{'*.amc'},@getAMCLengthInSeconds,true,[],param1,...,paramn);
30
31 if (startdir(end)~='\')
32 startdir = [startdir '\'];
33 end
34
35 files = dir(startdir);
36 if (isempty(strmatch('skip',{files.name},'exact'))) % only process dir if no "skip" file is present!
37 disp(['Entered directory ' startdir]);
38 files = [];
39 for k=1:length(filetypes)
40 fls = dir([startdir filetypes{k}]);
41 files = [files; fls];
42 end
43
44 for k=1:length(files)
45 disp(['Processing file ' files(k).name]);
46 if (nargin>5)
47 args = horzcat({startdir files(k).name}, varargin); % changed by -fk to pass both dir + filename
48 else
49 args = {startdir files(k).name}; % changed by -fk to pass both dir + filename
50 end
51 a = feval(func,args{:});
52 y = [y; a];
53 end
54
55 if (recursive)
56 dirs = dir(startdir);
57 isdir = cell2mat({dirs.isdir});
58 dirs = dirs(find(isdir));
59 for k = 1:length(dirs)
60 if (~strcmp(dirs(k).name,'.') & ~strcmp(dirs(k).name,'..'))
61 y = forAllFilesInDir([startdir dirs(k).name], filetypes, func, recursive, y, varargin{:});
62 end
63 end
64 end
65 disp(['Left directory ' startdir]);
66 else
67 disp(['Skipped directory ' startdir]);
68 end