up one directory's folder name from current directory

54 views (last 30 days)
I have directory path below
/Volumes/GoogleDrive/Shared /Mesure_geo/Temp/delete/AGING/LKG0.3V'
I need to get two string variables
LKG0.3V
AGING
yes I wanna use the current folder name and previous folder name (up one directory to AGING) in MatLab

Accepted Answer

Image Analyst
Image Analyst on 25 Aug 2020
See my utility:
%==========================================================================================================================
% Gets the folder one level up. If startingFolder is a filename with an extension it gets the containing folder and the child folder is null.
% Returns null for both if the folder does not exist, and it's not a filename either.
function [parentFolder, childFolder] = GetParentFolder(startingFolder)
parentFolder = []; % Initialize
childFolder = [];
try
if isfolder(startingFolder)
[parentFolder, childFolder, ext] = fileparts(startingFolder);
% Need to append extension for rare cases where deepest folder has a dot in the name.
childFolder = [childFolder, ext];
elseif isfile(startingFolder)
% It's a filename, not a folder. Need to change otherwise childFolder will be returned as the base file name.
[parentFolder, childFolder, ext] = fileparts(startingFolder);
childFolder = []; % No child folder since it's a filename, not a folder name.
end
catch ME
message = sprintf('Error in GetParentFolder():\n%s', ME.message);
uiwait(msgbox(message));
end
return; % from GetParentFolder
end

More Answers (0)

Categories

Find more on Search Path in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!