/MatlabCode/branches/Greg's Branch/Analysis/Sandbox/Greg/subdir.m

http://horwitzlab.googlecode.com/ · MATLAB · 110 lines · 43 code · 10 blank · 57 comment · 9 complexity · ea2770171b77579dbbeb89d671eb0b60 MD5 · raw file

  1. function varargout = subdir(varargin)
  2. %SUBDIR Performs a recursive file search
  3. %
  4. % subdir
  5. % subdir(name)
  6. % files = subdir(...)
  7. %
  8. % This function performs a recursive file search. The input and output
  9. % format is identical to the dir function.
  10. %
  11. % Input variables:
  12. %
  13. % name: pathname or filename for search, can be absolute or relative
  14. % and wildcards (*) are allowed. If ommitted, the files in the
  15. % current working directory and its child folders are returned
  16. %
  17. % Output variables:
  18. %
  19. % files: m x 1 structure with the following fields:
  20. % name: full filename
  21. % date: modification date timestamp
  22. % bytes: number of bytes allocated to the file
  23. % isdir: 1 if name is a directory; 0 if no
  24. %
  25. % Example:
  26. %
  27. % >> a = subdir(fullfile(matlabroot, 'toolbox', 'matlab', '*.mat'))
  28. %
  29. % a =
  30. %
  31. % 67x1 struct array with fields:
  32. % name
  33. % date
  34. % bytes
  35. % isdir
  36. %
  37. % >> a(2)
  38. %
  39. % ans =
  40. %
  41. % name: '/Applications/MATLAB73/toolbox/matlab/audiovideo/chirp.mat'
  42. % date: '14-Mar-2004 07:31:48'
  43. % bytes: 25276
  44. % isdir: 0
  45. %
  46. % See also:
  47. %
  48. % dir
  49. % Copyright 2006 Kelly Kearney
  50. %---------------------------
  51. % Get folder and filter
  52. %---------------------------
  53. error(nargchk(0,1,nargin));
  54. error(nargoutchk(0,1,nargout));
  55. if nargin == 0
  56. folder = pwd;
  57. filter = '*';
  58. else
  59. [folder, name, ext] = fileparts(varargin{1});
  60. if isempty(folder)
  61. folder = pwd;
  62. end
  63. if isempty(ext)
  64. if isdir(fullfile(folder, name))
  65. folder = fullfile(folder, name);
  66. filter = '*';
  67. end
  68. else
  69. filter = [name ext];
  70. end
  71. end
  72. %---------------------------
  73. % Search all folders
  74. %---------------------------
  75. pathstr = genpath(folder);
  76. seplocs = findstr(pathstr, pathsep);
  77. loc1 = [1 seplocs(1:end-1)+1];
  78. loc2 = seplocs(1:end)-1;
  79. pathfolders = arrayfun(@(a,b) pathstr(a:b), loc1, loc2, 'UniformOutput', false);
  80. Files = [];
  81. for ifolder = 1:length(pathfolders)
  82. NewFiles = dir(fullfile(pathfolders{ifolder}, filter));
  83. if ~isempty(NewFiles)
  84. fullnames = cellfun(@(a) fullfile(pathfolders{ifolder}, a), {NewFiles.name}, 'UniformOutput', false);
  85. [NewFiles.name] = deal(fullnames{:});
  86. Files = [Files; NewFiles];
  87. end
  88. end
  89. %---------------------------
  90. % Output
  91. %---------------------------
  92. if nargout == 0
  93. if ~isempty(Files)
  94. fprintf('\n');
  95. fprintf('%s\n', Files.name);
  96. fprintf('\n');
  97. end
  98. elseif nargout == 1
  99. varargout{1} = Files;
  100. end