function filelist = filehound(filespec)
%============================================================================
% syntax:
% filelist = filehound(filespec)
% or: filelist = filehound
% for default search for *.m files
%
% description : recursively examines directories for presence of files
% matching the filter defined by input parameter 'filespec'
%
% parameters in : filespec = (optional) filter for filenames default = '*.m'
%
% parameters out: filelist = cell array with paths for specified files
%
% filename : filehound.m
%
%============================================================================
%============================================================================
% author : Ronald Ouwerkerk, Johns Hopkins Univ, dept Radiology
% rouwerke@mri.jhu.edu
%
% started writing : 2001 last update : 20-02-2001
%
%============================================================================
global fout
%============================================================================
% set default
%============================================================================
if nargin == 0
filespec = '*.m'
end;
%============================================================================
% set directory separator for computer type
%============================================================================
comp = computer;
comp = comp(1:2);
switch(comp)
case 'SU',
usep = '/';
case 'SO',
usep = '/';
case 'SG',
usep = '/';
case 'AL',
usep = '/';
case 'DE',
usep = '/';
case 'HP',
usep = '/';
case 'IB',
usep = '/';
case 'VA',
usep = '.';
case 'PC',
usep = '\';
case 'MA',
usep = ':';
otherwise,
usep = 'xx';
end
olddir = pwd;
%============================================================================
% Get parent directory to search and filename for collected files
%============================================================================
fileuititle = 'Select directory and filename for report';
[filename, pathname] = uiputfile('*.txt', fileuititle)
if isempty(filename)
filename = 'report.txt'
pathname = pwd
else
eval(['cd 'pathname]);
end;
%============================================================================
% create filename for collected files delete old copies
%============================================================================
topdir = pwd;
outfilename = [topdir, usep, filename]
if exist(outfilename, 'file')
eval(['delete ',outfilename]);
end;
%============================================================================
% recursively scan all subdirectories
%============================================================================
filelist = [];
fout = fopen(outfilename,'a');
filelist = scandir(filelist, filespec);
fclose(fout);
eval(['cd 'olddir]);
%============================================================================
% END main
%============================================================================
%============================================================================
% local function scandir
% Scans the present directory and recursively scans all directories below
%============================================================================
function filelist = scandir(filelist, filespec)
%============================================================================
% define the global file ID
%============================================================================
global fout
%============================================================================
% define set of leagl chars for dirnames
%============================================================================
legalchar = '-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
%============================================================================
% get dir list
%============================================================================
thisdir = pwd;
thisdirlist = dir(thisdir);
%============================================================================
% find files (not isdir)
%============================================================================
kdir = [thisdirlist(:).isdir];
allfilelist = thisdirlist(~kdir);
nfiles = length(allfilelist);
%============================================================================
% files matching the pattern of filespec
%============================================================================
wildcardpos = findstr(filespec, '*');
k= 1:length(filespec);
k = k(k~=wildcardpos);
filefilter = filespec(k);
listpos = length(filelist);
count = 0;
for filei= 1:nfiles
fname = allfilelist(filei).name;
if findstr(filefilter, fname) & (length(fname) > length(filefilter))
filestr = sprintf('%s/%s', thisdir, fname);
count = count+1;
filelist{listpos + count} = filestr;
fprintf(fout,'%s\n', filestr);
end
end;
%============================================================================
% find subdirectories
%============================================================================
kdir = [thisdirlist(:).isdir];
thisdirlist = thisdirlist(kdir);
ndirs = length(thisdirlist);
%============================================================================
% scan all subdirectories recursively
%============================================================================
for diri= 1:ndirs
subdirname = thisdirlist(diri).name;
if ~strcmp(subdirname, '.') & ~strcmp(subdirname, '..')
ipos = ismember(subdirname, legalchar);
if length(subdirname(ipos)) < length(subdirname)
msgstr = sprintf('Skipped directory %s because its name', subdirname);
msgstr = sprintf('%s contains illegal characters\n',msgstr);
disp(msgstr)
else
cmdstr = ['cd ', subdirname];
disp(cmdstr);
eval(cmdstr);
filelist = scandir(filelist, filespec);
cmdstr = ['cd ', thisdir];
disp(cmdstr);
eval(cmdstr);
end;
end
end;
%============================================================================
%END local function scandir
%============================================================================