function [files]=getfiles(directory,searchstring);
%function [files]=getfiles(directory,searchstring);
%
% Gets files in a certain directory which match a certain searchstring

% ---------------------------------
% Copyright (c) 2002 Serge Dumoulin
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License version 2
% as published by the Free Software Foundation.
% ---------------------------------


if nargin < 2 | isempty(searchstring),
   searchstring='*';
end;
if nargin < 1 | isempty(directory),
   directory=pwd;
end;

div=filesep;
if directory(length(directory))~=div,
   directory=[directory div];
end;

files=[];
for n=1:size(searchstring,1),
   totstring=[directory deblank(searchstring(n,:))];

   d=dir(totstring);
   % convert to char
   files=strvcat(files,char(d.name));
end;
% sort because dir does not preserve the order
files=sortrows(files);

if files(1,:) == ['.' blanks(size(files,2)-1)],
   files=files(2:end,:);
end;
if files(1,:) == ['..' blanks(size(files,2)-2)],
   files=files(2:end,:);
end;

% add directory
if ~isempty(files),
   if files(1,1)~=div & files(1,1)~='.',
      dirs=char(ones(size(files,1),1)*double(directory));
      files=[dirs files];
   end;
end;