From: Walter Roberson on
us wrote:
> "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message
> <hvdroo$8ec$1(a)fred.mathworks.com>...
>> Dear Graham!
>>
>> > > currentDirectory = pwd
>> > > [upperPath, deepestFolder, ~] = fileparts(currentDirectory)
>>
>> I'd omit the trailing "~":
>> [upperPath, deepestFolder] = fileparts(currentDirectory)
>> It works, but I do not see the benefits.

> well... what's the fuss about these output args(?)...
>
> one of the (shorter) solutions
>
> cd
> % ans = F:\usr\r2010a
> fp=fileparts(cd)
> % fp = F:\usr

Except the name of the current folder would be 'r2010a' in that case, not
'F:\usr' .
From: Jan Simon on
Dear Graham!

> [upperPath, deepestFolder] = fileparts(currentDirectory)

My first idea fails if the folder name contains a dot. Better:
[upperPath, deepestFolder, Ext] = fileparts(currentDirectory)
deepestFolder = [deepestFolder, Ext];

What about:
currentDirectory = cd;
SepInd = findstr(currentDirectory, filesep);
nSep = length(SepInd);
if nSep > 0
deepestFolder = currentDirectory(SepInd(nSep):length(currentDirectory));
else
deepestFolder = '';
end

Do folder names on Linux include a trailing file separator?

Jan
From: Walter Roberson on
Jan Simon wrote:

> Do folder names on Linux include a trailing file separator?

POSIX defines the trailing file separator on directory names to be optional
and redundant copies may be given

abc///////def/

is the same as the directory

abc/def


The one exception to this is that

//abc

is considered to be potentially different than

/abc

The POSIX rule is that two (or more) leading // signals a file-system escape.
Windows follows this in its use of //ServerName/folder to indicate a remote
system.
From: Jan Simon on
Dear Walter,

> POSIX defines the trailing file separator on directory names to be optional
> and redundant copies may be given
> abc///////def/
> is the same as the directory
> abc/def
>
> The one exception to this is that
> //abc
> is considered to be potentially different than
> /abc
> The POSIX rule is that two (or more) leading // signals a file-system escape.
> Windows follows this in its use of //ServerName/folder to indicate a remote
> system.

If I understand correctly, there is no parent folder of "//ServerName/folder". Then it would be safe to exclude all trailing file separators at first.

function Folder = LastFolder(Directory)
isSep = (Directory== filesep);
if any(isSep)
lastNoSep = find(~isSep, 1, 'last');
if isempty(lastNoSep)
Folder = ''; % Catch / on linux
else
isSep(lastNoSep + 1:end) = 0;
lastSep = find(isSep, 1, 'last');
Folder = Directory((lastSep + 1):lastNoSep);
end
else
Folder = '';
end
return;

I cannot run Matlab at the moment, so this is not tested.
What might be the valid anser for the input "/" on linux and "C:\" on Windows?

Jan
From: Walter Roberson on
Jan Simon wrote:

> function Folder = LastFolder(Directory)

> What might be the valid anser for the input "/" on linux and "C:\" on
> Windows?

I don't know for sure on Windows.

On POSIX systems, the parent of "/" is "/"