function filerename(varargin)
% FILERENAME Rename file(s) and/or directory(ies) on a specified path.
%
% Syntax
%   RENAME('source','newname')
%   RENAME('source','newname','type')
%
% Description
%   RENAME('source','newname') renames files and directories from
%   the original name to a new name. Note that the file extensions aew not needed
%   in the source and new name character strings. Use the wildcard * at the end of source 
%   to rename multiple files/directories
%
%   RENAME('source','newname','type') renames only files or directories from
%   the original name to a new name. Available 'type' options are :
%       'dir' 	- only the directory names
%   	'file'	- only the file names
%
% Examples
%   1) filerename('C:\Data\Projects\AUTOFlux_programs\flux\Xcruise2009_Rsclear_1.m','Test2009')
%      |-->this renames M-file 'C:\Data\Projects\AUTOFlux_programs\flux\Xcruise2009_Rsclear_1.m' to 
%          'C:\Data\Projects\AUTOFlux_programs\flux\Test2009_Rsclear_1.m'
%
%   2) filerename('C:\Data\Projects\AUTOFlux_programs\flux\*Xcruise2009*','Test2009')
%      |-->this renames all files and folders, contained into directory 'C:\Data\Projects\AUTOFlux_programs\flux\',
%          by replacing the string 'Xcruise2009' with 'Test2009'
%
%   3) filerename('C:\Data\Projects\AUTOFlux_programs\flux\*Xcruise2009*.m','Test2009')
%       or
%      filerename('C:\Data\Projects\AUTOFlux_programs\flux\*Xcruise2009*','Test2009','file')
%      |-->this renames only all the M-files, contained into directory 'C:\Data\Projects\AUTOFlux_programs\flux\',  
%          by replacing the string 'Xcruise2009' with 'Test2009'
%   
%   4) filerename('C:\Data\CRUISES\WHOTS09\Kilo_Moana\flux\Processed_images\*191*.jpg','Test2009')
%      |-->this renames all JPEG files, contained into directory 'C:\Data\CRUISES\WHOTS09\Kilo_Moana\flux\Processed_images\',
%          with day-of-year '191' to 'Test2009'. 'son2_spectrum19100.jpg' will then become 'son2_spectrumTest200900.jpg'
%
% NOTES:
%   1) Note that you cannot specify a new drive or path for your destination.
%   2) Only the file name is changed by this function. The modification date timestamp of the file remains the same
%   3) filerename will work only within the specified directory. For instance, filerename('C:\Xcruise_2009','Test2009') will 
%      only rename the folder 'Xcruise_2009' to 'Test2009', but it won't rename the files contained inside that folder. 
%      To change the M-file names containing the string 'Xcruise_2009' inside the folder 'Xcruise_2009', type  
%      filerename('C:\Xcruise_2009\*Xcruise_2009*.m','Test2009')
%
% Ludovic Bariteau on 09/21/2009
% Revision history, original version 09/21/2009
% Changed help content on 09/28/09

%do some checks on inputs
switch nargin
    case 1
        error('Use at least 2 inputs for filerename(see help filerename)'); 
    case 2
        if (~ischar(varargin{1}) || ~ischar(varargin{2}))
            error('This function requires string inputs.');
        elseif ~isempty(fileparts(varargin{2}))
            error('You cannot specify a new drive or path for your new filename.');    
        end    
    case 3
        if (~ischar(varargin{1}) || ~ischar(varargin{2}))
            error('This function requires string inputs.');
        elseif ~isempty(fileparts(varargin{2}))
            error('You cannot specify a new drive or path for your new filename.');    
        elseif ~ischar(varargin{3})
            error('This function requires string inputs.');
        elseif ~ismember(varargin{3},{'file','dir'})
            error(['Invalid parameter: ''' varargin{3} '''. Use ''file'' or ''dir'''])
        end    
    otherwise  %case nargin>3
        error('Use 2 or 3 inputs only for filerename(see help filerename)'); 
end;

%Gets parts of file name and path
[path, oldfilename] = fileparts(varargin{1});

%Defines old filename and new filename
oldname = strtok(oldfilename, '*');
newname=varargin{2};

%List of files in the specified directory
files=dir(varargin{1});

%If activated, selects file(s) or directory(ies) to rename
if nargin==3
    switch lower(varargin{3})
       case 'file'     %select only files to rename
          files=files(~cellfun(@(x) x==1,{files(:).isdir}));
       case 'dir'      %select only directories to rename
          files=files(cellfun(@(x) x==1,{files(:).isdir}));
    end;  
end;

%Rename file(s) and/or directory(ies)
for i=1:length(files),
    newfilename=strrep(files(i).name,oldname,newname);
    if ispc
        dos(['ren ' fullfile(path,files(i).name) ' ' newfilename]);
    else
        system(['mv ' fullfile(path,files(i).name) ' ' newfilename]);
    end;
end
