Delphi 级别,我的计算机目录的深度

Delphi 级别,我的计算机目录的深度,delphi,delphi-xe2,Delphi,Delphi Xe2,我有两个目录路径: 路径1=C:\Users\SomeUser\Documents\Embarcadero\Studio\ 路径2=C:\Users\SomeUser\Documents\ 如您所见,路径1比路径2深两层 换句话说,路径1的深度是6,路径2的深度是4 我需要一个可以计算这个深度的函数 编辑: 这是另一个误解我问题的人的例子 Path A :="c:\" {This is level 1 or depth 1} Path B := "c:\foo" {This is lev

我有两个目录路径: 路径1=C:\Users\SomeUser\Documents\Embarcadero\Studio\ 路径2=C:\Users\SomeUser\Documents\ 如您所见,路径1比路径2深两层 换句话说,路径1的深度是6,路径2的深度是4 我需要一个可以计算这个深度的函数

编辑: 这是另一个误解我问题的人的例子

Path A :="c:\"  {This is level 1 or depth 1}  
Path B := "c:\foo"  {This is level 2 or depth 2}  
Path C := "c:\bar"  {This is level 2 or depth 2}  
Path D := "c:\foo\folder1"  {this is level 3 or depth 3}  
Path E := "c:\bar\folder2"  {this is level 3 or depth 3}  
我想现在很清楚,当我说深度时,我的意思是,如果我在驱动器C中,我应该潜入多深才能到达最终文件夹 为什么我需要这个? 因为在我的程序中,用户根据给定的基本目录设置深度,我必须搜索并找到该基本目录根目录中的所有文件,如果深度为1,例如深度为2,我也必须搜索根目录中的任何文件夹,如果它是3,我将搜索一个文件夹中的任何文件,这个文件夹位于另一个文件夹的根目录中 我需要的距离或深度差也是我程序中另一个功能所需要的,可以计算为

abs(depthFolder1 - depthFolder 2)

获取两条路径之间的差值并计算\

未测试,因为没有打开Delphi


首先,我感谢所有其他的答案 但在其他人公布答案之前,我已经找到了我的答案

function findPathDepth(path: String): integer;
var
  I: Integer;
  depthCounter: Integer;
begin
  depthCounter := 0;
  // path.Length - 2 in case there is an extra "\" at the end
  for I := 0 to path.Length - 2 do
    if (path[i] = '\') then
      inc(depthCounter);

  Result := depthCounter;
end;

function findPathDepthDistance(path1: String; path2: String): integer;
begin
  if (ExtractFileDrive(path1) = ExtractFileDrive(path2)) then
      Result := abs(findPathDepth(path1) - findPathDepth(path2));
  else 
      Result := -1;
end;  
再次感谢

function GetDirLevel(aDirectory: string): integer;
var
  a_Index: integer;
begin
  Result := 0;
  aDirectory := IncludeTrailingPathDelimiter(aDirectory);
  for a_Index := 1 to Length(aDirectory) do
    if IsPathDelimiter(aDirectory, a_Index) then
      inc(Result);
end;

function GetComparedLevel(aBase, aDirectory: string): integer;
begin
  aBase := IncludeTrailingPathDelimiter(aBase);
  aDirectory := ExtractRelativePath(aBase, aDirectory);
  if POS('..', aDirectory) = 1 then
    Result := -1
  else
    Result := GetDirLevel(aDirectory);
end;
这将使用与基础的相对路径来确定标高。因此,如果您的目录不在BaseDir中,它将返回-1

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := IntToStr(GetComparedLevel('c:\temp', 'c:\temp\level2\level3\level4'));//will return 3
  Label2.Caption := IntToStr(GetComparedLevel('c:\temp\level', 'c:\temp\level2\level3\level4'));//will return -1

end;

C:\foo和C:\bar之间的预期距离是多少?在C:\foo和D:\baz之间如何?@rob00显然不正确,那么C:\foo和C:\foo呢?我不太清楚,尽管它确实是函数返回的结果。C:\foo\qux和C:\bar之间的距离是多少?把它想象成一棵树,我会说距离是3。绘制节点,然后计算从一片叶子到另一片叶子的边数。我认为从C:\到d:\的距离没有定义,因为树没有根节点。塞塔克的例子显然是零。@RobKennedy,在C:\和D:\之间有一个根音符,即MyComputer。这取决于…看起来区分大小写:-PYes,这是我的终极路径距离计数库的alpha预发行版。您的代码认为C:\foo\bar\和C:\bar\之间的距离是1。@Sertac:确实如此。我真想知道他认为两个不相关的目录之间的深度是多少。他说它是零,@Rudy,显然。@SertacAkyuz我希望它返回1,为了我的目的,1是正确的答案,在我的书中,如果c:\是级别1,其中任何其他文件夹都是级别2,如果这些文件夹中有其他文件夹,所有的问题都是3级的,这是我需要知道的,这是我的函数返回的,如果其他人需要其他东西,他们可以根据自己的需要更改。你真的应该删除整个问题,因为它对任何人都没有用。在问之前你应该仔细想想。
function GetDirLevel(aDirectory: string): integer;
var
  a_Index: integer;
begin
  Result := 0;
  aDirectory := IncludeTrailingPathDelimiter(aDirectory);
  for a_Index := 1 to Length(aDirectory) do
    if IsPathDelimiter(aDirectory, a_Index) then
      inc(Result);
end;

function GetComparedLevel(aBase, aDirectory: string): integer;
begin
  aBase := IncludeTrailingPathDelimiter(aBase);
  aDirectory := ExtractRelativePath(aBase, aDirectory);
  if POS('..', aDirectory) = 1 then
    Result := -1
  else
    Result := GetDirLevel(aDirectory);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := IntToStr(GetComparedLevel('c:\temp', 'c:\temp\level2\level3\level4'));//will return 3
  Label2.Caption := IntToStr(GetComparedLevel('c:\temp\level', 'c:\temp\level2\level3\level4'));//will return -1

end;