Author Topic: DirectoryIsRoot() function  (Read 862 times)

Bart

  • Hero Member
  • *****
  • Posts: 5800
    • Bart en Mariska's Webstek
Re: DirectoryIsRoot() function
« Reply #15 on: September 15, 2026, 10:15:36 pm »
Can't we check by its parent?

Code: Pascal  [Select][+][-]
  1. function FncDirIsRoot(const PrmDir: String): Boolean;
  2. var
  3.   dir: String;
  4. begin
  5.   dir := ExcludeTrailingPathDelimiter(PrmDir);
  6.   Result := (ExtractFilePath(dir) = dir);
  7. end;    

Nice idea, but it fails on
  • C: (returns True, which is only the case if curreent directory is C:\
  • C:\..\ (returns False, but C:\.. equals C:\, so should return True
  • \\server\\ (returns True, should be False)
  • \\server\share (returns False, should be True)
  • \\server\share\ (returns False, should be True)

Bart

Xenno

  • Guest
Re: DirectoryIsRoot() function
« Reply #16 on: September 16, 2026, 04:22:17 am »
Great info.
Thank you very much.

Bart

  • Hero Member
  • *****
  • Posts: 5800
    • Bart en Mariska's Webstek
Re: DirectoryIsRoot() function
« Reply #17 on: September 16, 2026, 01:58:27 pm »
I also tried something like:
Code: Pascal  [Select][+][-]
  1. function DirIsRoot(const PrmDir: String): Boolean;
  2. var
  3.   dir: String;
  4. begin
  5.   dir := ExpandFileName(ChompPathDelim(PrmDir)+'\..\');
  6.   Result := CompareText(dir, PrmDir) = 0;
  7. end;

But that one failse on '\\server\share' and 'C:\..\' as well.

So, for now it remains a bit convoluted.
I refactored it a bit (the repeated copy()/pos() part)..
It also had a bug in the sense that it returned True on '\\?\C:/' (which is not the same as 'C:\' since in this naming scheme all characters are meant to be literal.
That's a bit contrived though.

Bart

Cecolord

  • New member
  • *
  • Posts: 8
Re: DirectoryIsRoot() function
« Reply #18 on: October 28, 2026, 10:09:20 am »
I wanted to make a "deltree" program, since that does not ship with Windows anymore.
(And "del path /s/q" does not remove the then empty directories, whilst "rm path /s/q" does not remove non-empty directories.)
For that program I want to ask extra confirmation if one were to call "deltree c:\".
So I want to know if a directory is the root directory.
Sounds simple, but I managed to make it a bit complicated.
::) One solution

Code: [Select]
function PathCharLength(const S: String; const Index: Integer): Integer;
{ Returns the length in bytes of the character at Index in S.
  Notes:
  1. If Index specifies the last character in S, 1 will always be returned,
     even if the last character is a lead byte.
  2. If a lead byte is followed by a null character (e.g. #131#0), 2 will be
     returned. This mimics the behavior of MultiByteToWideChar and CharPrev,
     but not CharNext(P)-P, which would stop on the null. }
begin
  {$IFNDEF UNICODE}
  if IsDBCSLeadByte(Ord(S[Index])) and (Index < Length(S)) then
    Result := 2
  else
  {$ENDIF}
    Result := 1;
end;

function PathIsRooted(const Filename: String): Boolean;
{ Returns True if Filename begins with a slash or drive ('x:').
  Equivalent to: PathDrivePartLengthEx(Filename, True) <> 0 }
var
  Len, I: Integer;
begin
  Result := False;
  Len := Length(Filename);
  if Len > 0 then begin
    { \ or \\ }
    if PathCharIsSlash(Filename[1]) then
      Result := True
    else begin
      { x: }
      I := PathCharLength(Filename, 1) + 1;
      if (I <= Len) and (Filename[I] = ':') then
        Result := True;
    end;
  end;
end;

 ::) Two solution
 Please you set limmin of path
Code: [Select]
Function Path_min_isRoot(limmin:word):boolean;
cnt:=00;
  For j:=1 to length(path) do if (path[j]='\') then inc(cnt);
  result:=(cnt < limmin)
End;

 

TinyPortal © 2005-2018