function [NewFilePath]=filerename(filename,filepath,findstring,replacestring,matlabversion)

%  filerename

%   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.
%  

copyreplaceradio = findobj('tag','copyreplaceradio');
copyreplace = get(copyreplaceradio,'Value');

OldFilePath=[filepath,filename];
if isempty(findstr(filename,findstring))
    NewFilePath=OldFilePath;
    return
end
if isempty(filepath)
    [filepath,filename,ext,versn] = fileparts(filename);
    filename=[filename ext];
    filepath=[filepath,filesep];
end

NewFilePath=strrep(filename,findstring,replacestring);


if strcmp(computer,'PCWIN')
    if matlabversion > 6.1
        if copyreplace == 0 | isempty(copyreplace)
            oldpath = pwd;
            cd(filepath);
            dosstring = ['ren ',filename,' ',NewFilePath];
            dos(dosstring);
            cd (oldpath);
        else
            NewFilePath=[filepath,NewFilePath];
            copyfile(OldFilePath,NewFilePath,'writable');
        end
    else
        NewFilePath=[filepath,NewFilePath];
        copyfile(OldFilePath,NewFilePath,'writable');
        if copyreplace == 0 | isempty(copyreplace)
            delete(OldFilePath);
        end
    end
elseif isunix
    NewFilePath=[filepath,NewFilePath];
    if copyreplace == 0 | isempty(copyreplace)
        oldpath = pwd;
        cd(filepath);
        unixstring = ['rename ',findstring,' ',replacestring,' ',OldFilePath];
        result = unix(unixstring);
        cd (oldpath);
    else
        NewFilePath=[filepath,NewFilePath];
        copyfile(OldFilePath,NewFilePath,'writable');
    end
end
    
    

return




