Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if path points to a root folder using Delphi

Tags:

delphi

What is the best/easiest way to check if a certain path points to a drive's root?

I guess I could just check if path name ends with '\' or ':', or if the path is only 2 or three characters in length, but I was hoping there was some kind of standard "IsDriveRoot" function to check this.

Tx

UPDATE:

After searching through the Delphi help file I found the ExtractFileDrive() function which returns the drive portion of any given path.

Using that function I gues it's easy to write a little function to check if the original path is the same as the result of ExtractFileDrive(), which would mean that the original path had to be the drive's root.

Function IsDriveRoot(APath: string): Boolean;
begin
  Result := ((Length(APath) = 2) and (ExtractFileDrive(APath) = APath))
         or ((Length(APath) = 3) and ((ExtractFileDrive(APath) + '\') = APath));
end;

or

Function IsDriveRoot(APath: string): Boolean;
begin
  Result := ((Length(APath) = 2) and (Copy(APath,2,1) = ':'))
         or ((Length(APath) = 3) and (Copy(APath,3,1) = '\'));
end;

Something like that should do it....

I actually think the second example is simpler, and will probably end up using that one.

Thanks again to all who responded :)

like image 332
deonvn Avatar asked Feb 24 '11 13:02

deonvn


1 Answers

It seems GetVolumePathName can be quite helpful in your case.

like image 135
Andriy M Avatar answered Oct 18 '22 09:10

Andriy M