deonvn
deonvn

Reputation: 61

How to check if path points to a root folder using 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 :)

Upvotes: 6

Views: 2115

Answers (2)

Andriy M
Andriy M

Reputation: 77687

It seems GetVolumePathName can be quite helpful in your case.

Upvotes: 3

barti_ddu
barti_ddu

Reputation: 10299

You could utilize GetDriveType() call:

if GetDriveType(PChar(path)) <> DRIVE_NO_ROOT_DIR then
...

Upvotes: 0

Related Questions