我电脑目录的级别,深度

时间:2016-04-21 14:36:52

标签: 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 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)

4 个答案:

答案 0 :(得分:0)

获取两条路径之间的差异并计算\的数量。

未经测试,因为没有打开Delphi

uses Math;

function GetSamePart(const A,B: string): string;
var
  i: integer;
  MinLen: integer;
begin
  i:= 0;
  MinLen:= Min(Length(A), Length(B));
  while i < MinLen do begin
    if lowercase(A[i+1]) = Lowercase(B[i+1]) then inc(i)
    else break;
  end; {while}
  if i = 0 then Result:= ''
  else Result:= Copy(A, 1, i);
end;

function OccurrencesOfChar(const S: string; const C: char): integer;
var
  i: Integer;
begin
  result := 0;
  for i := 1 to Length(S) do
    if S[i] = C then
      inc(result);
end;

function CountPaths(const Pathname: string; PathChar: char = '\'): integer;
begin
  if PathName = '' then Exit(0);
  Result:= OccurrencesOfChar(PathName, PathChar);
  if PathName[Length(PathName)] <> PathChar then Inc(Result);
end;

function GetPathDistance(const A,B: string): integer;
var
  ADiff, BDiff: string;
  FirstDiff: integer;
begin
  FirstDiff:= Length(GetSamePart(A,B));
  if FirstDiff < Length(A) then ADiff:= Copy(A, FirstDiff+1, Length(A)-FirstDiff);
  if FirstDiff < Length(B) then BDiff:= Copy(B, FirstDiff+1, Length(B)-FirstDiff);
  Result:= CountPaths(ADiff) + CountPaths(BDiff);
end;

答案 1 :(得分:0)

const 
  Path1 = "C:\Users\SomeUser\Documents\Embarcadero\Studio\";
  Path2 = "C:\Users\SomeUser\Documents\";

var 
  sl1, sl2: iJclStringList;

procedure Init(out sl: iJclStringList; const CanonicalPath: string);
begin
  sl := JclStringList();
  sl.Split( AnsiUpperCase( CanonicalPath ), '\' );
  if sl.Count <= 0 then exit;
  if sl.Last = '' then sl.Delete( sl.LastIndex );
end;

var i: integer;

begin
  Init( sl1, Path1 );
  Init( sl2, Path2 );

  i := 0;
  while (i < sl1.Count) and (i < sl2.Count)
    and (sl1[i] = sl2[i]) 
  do
      Inc(i);

  // now i maps to the first different element
  Difference := Max(sl1.Count, sl2.Count) - i;

  sl1 := nil;
  sl2 := nil;
end;

http://wiki.delphi-jedi.org/wiki/Category:IJclStringList_Methods

答案 2 :(得分:0)

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;

这使用RelativePath到基数来确定级别。因此,如果您的目录不在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;

答案 3 :(得分:-2)

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

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;  

再次感谢。