image thumbnail

Level folder string

by

 

13 Feb 2013 (Updated )

The function returns user chosen level folder name out of path provided.

nLevelFolderName=levelFolderName(userFullPath, nLevel)
function nLevelFolderName=levelFolderName(userFullPath, nLevel)
%% levelFolder
% The function returns user chosen level folder name out of full path provided.
%  
%% Syntax:
%  nLevelFolderName=levelFolder(userFullPath, nLevel);
%  nLevelFolderName=levelFolder(userFullPath);
%  
%% Description:
% Folder name carries some description of the files stored in the folder. However, it is
%   sometimes interesting to use the name of a folder on a higher level. This function
%   returns the string user chosen level folder, eliminating the need to present the
%   whole path in cases it is obsolete.
%  
%% Input arguments (defaults exist):
%  userFullPath- a string of the relevant path. Should be a path to a
%   folder, and not a file.
%  nLevel- an integer specifying the level of the folder the user is interested to
%   retrieve. In case no value is specified 1 would be the default value. Here 1 is the
%   current (last) folder.
%  
%% Output arguments:
%  nLevelFolderName- a string specifying the nLevel folder name.
%  
%% Issues & Comments:
%  Providing a path to a nonexistent file, may result in an output, where
%   file anme will be treated as a folder name.
%  
%% Example:
% currFolder=pwd;
% nLevel=1;
% fprintf('\n&&&&&&&&&&&&&&&&&&&&&&&&\n\n');
% while ~strcmpi(currFolder, levelFolderName(currFolder, nLevel))
%     fprintf('Level %d- folder name- %s\n', nLevel, levelFolderName(currFolder, nLevel));
%     nLevel=nLevel+1;
% end
% 
% fprintf('Level %d- full path - %s\n', nLevel, levelFolderName(currFolder, nLevel));
%  
%% See also:
%  fileparts
%  
%% Revision history
% First version: Nikolay S. 2013-02-12.
% Last update:   Nikolay S. 2013-02-14.
%  
%% *List of Changes*:
% 2013-02-14- Changed the way files are treated
%

%% Deafult params
if nargin < 2
    nLevel=1;
end

%% Program start
% get the path, disregarding file name and extention, if such a file exists
if exist(userFullPath, 'file')==2
    [userFullPath, ~, ~]=fileparts(userFullPath);
end
% Verify last char if file-seperator
if ~strcmpi( userFullPath(end), filesep )
    userFullPath=cat(2, userFullPath, filesep );
end

iFileSep=strfind( userFullPath, filesep ); % Find all file-seperator locations

% % Comented out, as Folder name can have dots in it too
% iDot=strfind( userFullPath, '.' ); 
% % in case a full path including file name was provided
% if ~isempty(iDot) % remove the last filesep added before
%     iFileSep=iFileSep(iFileSep < iDot);
% end


nLevel=nLevel-1; % starting from 1, not 0
nFileSep=length(iFileSep);
if nLevel > (nFileSep-1) || nLevel<0 % if too deep level is defined- return whole path
    nLevelFolderName=userFullPath(1:end-1); %disregard the last file seperator
elseif nLevel == (nFileSep-1)
    nLevelFolderName=userFullPath( 1: (iFileSep(1)-1) );
else
    % Otherwise, return the appropriate folder name.
    nLevelFolderName=userFullPath( (1+iFileSep(end-nLevel-1)): (iFileSep(end-nLevel)-1) );
end

Contact us