function [filenamecell,filepathcell]=lookindir(filepath,filter,filenamecell,filepathcell,subfolderstatus,filter_ex,endofnamestatus,endofnameexstatus,searchpart,logictype)

%   lookindir

%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.                                                                      

if nargin < 10; searchpart = 1;end
if nargin < 9;logictype = 1;end
if nargin < 8; endofnameexstatus = 0;end
if nargin<7; endofnamestatus = 0;end

if logictype==1
    logicmod = 1;
elseif logictype ==2
    logicmod = 0;
else
    logicmod = 1;
end

clc
fprintf(1,'%s',['Actual path: ',filepath]);

if ~isempty(filter_ex)
    findstr = filter_ex(1:strfind(filter_ex,char(32))-1);
    replacestring = filter_ex((strfind(filter_ex,char(32))+1):length(filter_ex));
end


try
    cd(filepath);
catch
    fprintf('\n');
    fprintf(1,'%s',['Directory ',filepath,' could not be opened...']);
    return
end   

if endofnamestatus
    lengthfilter = size(filter,2);
end
if endofnameexstatus
    lenghfilter_ex = size(filter_ex,2),
end

filepathcelltmp=dir;
subfoldervec=[];
subfolderzaehler=1;
for j=1:size(filepathcelltmp,1)
    if ~strcmp(filepathcelltmp(j).name,'.') & ~strcmp(filepathcelltmp(j).name,'..')
        %check if its a subdirectory
        if ~strcmp(filepath(end),filesep)
            filepath = [filepath,filesep];
        end
        if exist([filepath,filepathcelltmp(j).name],'dir')~=7
            %filter
            if ~isempty(filter)
                found = 0;
                %compare strings
                if searchpart==1%filename
                    if xor(~isempty(strfind(filepathcelltmp(j).name,filter)),~logicmod) %contains vs notcontain
                        if ~endofnamestatus
                            if ~isempty(filenamecell{1})
                                filenamecell{end+1}=filepathcelltmp(j).name;
                                filepathcell{end+1}=filepath;
                                found = 1;
                            else
                                filenamecell{1}=filepathcelltmp(j).name;
                                filepathcell{1}=filepath; 
                                found = 1;
                            end
                            if ~isempty(filter_ex)
                                filenametmp = strrep(filepathcelltmp(j).name,findstr,replacestring);
                                if found == 1 & exist([filepath,filenametmp])==2
                                    filenamecell{end+1}=filenametmp;
                                    filepathcell{end+1}=filepath;
                                end
                            end
                        else
                           if strfind(filepathcelltmp(j).name,filter)+lengthfilter-1 == size(filepathcelltmp(j).name,2)
                                if ~isempty(filenamecell{1})
                                    filenamecell{end+1}=filepathcelltmp(j).name;
                                    filepathcell{end+1}=filepath;
                                    found = 1;
                                else
                                    filenamecell{1}=filepathcelltmp(j).name;
                                    filepathcell{1}=filepath; 
                                    found = 1;
                                end
                                if ~isempty(filter_ex)
                                    filenametmp = strrep(filepathcelltmp(j).name,findstr,replacestring);
                                    if found == 1 & exist([filepath,filenametmp])==2
                                        filenamecell{end+1}=filenametmp;
                                        filepathcell{end+1}=filepath;
                                    end
                                end
                            end
                        end
                    end
                elseif searchpart == 2 %extension
                    [pathstr,name,ext,versn]= fileparts(filepathcelltmp(j).name);
                    if xor(~isempty(strfind(ext,filter)),~logicmod) %contains vs notcontain
                        if ~endofnamestatus
                            if ~isempty(filenamecell{1})
                                filenamecell{end+1}=filepathcelltmp(j).name;
                                filepathcell{end+1}=filepath;
                                found = 1;
                            else
                                filenamecell{1}=filepathcelltmp(j).name;
                                filepathcell{1}=filepath; 
                                found = 1;
                            end
                            if ~isempty(filter_ex)
                                filenametmp = strrep(filepathcelltmp(j).name,findstr,replacestring);
                                if found == 1 & exist([filepath,filenametmp])==2
                                    filenamecell{end+1}=filenametmp;
                                    filepathcell{end+1}=filepath;
                                end
                            end
                        else
                           if strfind(filepathcelltmp(j).name,filter)+lengthfilter-1 == size(filepathcelltmp(j).name,2)
                                if ~isempty(filenamecell{1})
                                    filenamecell{end+1}=filepathcelltmp(j).name;
                                    filepathcell{end+1}=filepath;
                                    found = 1;
                                else
                                    filenamecell{1}=filepathcelltmp(j).name;
                                    filepathcell{1}=filepath; 
                                    found = 1;
                                end
                                if ~isempty(filter_ex)
                                    filenametmp = strrep(filepathcelltmp(j).name,findstr,replacestring);
                                    if found == 1 & exist([filepath,filenametmp])==2
                                        filenamecell{end+1}=filenametmp;
                                        filepathcell{end+1}=filepath;
                                    end
                                end
                            end
                        end
                    end
                end
            
            %nofilter
            else
                found = 0;
                if ~isempty(filenamecell{1})
                        filenamecell{end+1}=filepathcelltmp(j).name;
                        filepathcell{end+1}=filepath;
                        found = 1;
                else
                        filenamecell{1}=filepathcelltmp(j).name;
                        filepathcell{1}=filepath;
                        found = 1;
                end
                
                if ~isempty(filter_ex)
                    filenametmp = strrep(filepathcelltmp(j).name,findstr,replacestring);
                    if found == 1 & exist([filepath,filenametmp])==2
                        filenamecell{end+1}=filenametmp;
                        filepathcell{end+1}=filepath;
                    end
                end
                    
            end
        else
            subfoldervec(subfolderzaehler)=j;
            subfolderzaehler=subfolderzaehler+1;
        end
    end
end
if subfolderstatus==1
    for i=1:subfolderzaehler-1
        if strcmp(computer,'PCWIN')
            [filenamecell,filepathcell]=lookindir([filepath,filepathcelltmp(subfoldervec(i)).name,'\'],filter,filenamecell,filepathcell,subfolderstatus,filter_ex,endofnamestatus,endofnameexstatus);
        elseif strcmp(computer, 'GLNX86')
            [filenamecell,filepathcell]=lookindir([filepath,filepathcelltmp(subfoldervec(i)).name,'/'],filter,filenamecell,filepathcell,subfolderstatus,filter_ex,endofnamestatus,endofnameexstatus);
        end    
	end
end
return