您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

12种方法返回2个文件路径之间的公共基路径ExtractBasePath(2)

时间:2011/9/3 15:31:41 点击:

方法四:Josip Brozovic

function ExtractBasePath( const path1, path2 : string ): string;
var
  s_shorter, s_longer: string;
  j: integer;
begin
  if Length( path1 ) > Length( path2 ) then
  begin
    s_longer := path1;
    s_shorter := path2;
  end
else
begin

    s_longer := path2;
    s_shorter := path1;
  end;

  result := s_shorter;

  for j := 1 to Length( s_shorter ) do
  begin
    if UpCase( path1[ j ] ) <> UpCase( path2[ j ] ) then
    begin
      Delete( result, j, MaxInt ) ;
      break;
    end;
  end;

  if ( result = s_shorter ) and
     ( Length( s_longer ) > Length( s_shorter )) and
     ( s_longer[ Length( s_shorter ) + 1 ] = '\' ) then
  begin
      result := result + '\';
  end;

  result := ExtractFilePath( result ) ;
end;

方法五:Korhan

function ExtractBasePath(const path1, path2 : string) : string;
var
  minLength : Integer;
  cnt : Integer;
  samePart : String;
begin
  if Length(path1) < Length(path2) then
    minLength := length(path1)
  else
    minLength := length(path2) ;

  Result := '';
  samePart := '';

  for cnt := 1 to minLength do
  begin
    if path1[cnt] = path2[cnt] then
    begin
      samePart := samePart + path1[cnt];
      if (path1[cnt] = '\') or ( (Length(path1) = Length(path2)) and (minLength = cnt) ) then
      begin
        Result := Result + samePart;
        samePart := '';
      end;
    end
    else

      Break;
  end;
end;

方法六:Jeff Lawson

function ExtractBasePath(const Path1, Path2: string): string;
var
  P1, P2,
  Dir1, Dir2,
  Base: string;
begin
  Base := '';
  P1 := LowerCase(Path1) ;
  P2 := LowerCase(Path2) ;

  if (ExtractFileExt(P1) = '') and (P1[Length(P1) - 1] <> '\') then P1 := P1 + '\';

  if (ExtractFileExt(P2) = '') and (P2[Length(P2) - 1] <> '\') then P2 := P2 + '\';

  while (P1 <> '') and (P2 <> '') do
  begin
    Dir1 := Copy(P1, 0, AnsiPos('\', P1)) ;
    Dir2 := Copy(P2, 0, AnsiPos('\', P2)) ;
    P1 := Copy(P1, Length(Dir1) + 1, Length(P1) - Length(Dir1) + 1) ;
    P2 := Copy(P2, Length(Dir2) + 1, Length(P2) - Length(Dir2) + 1) ;
    if Dir1 <> Dir2 then Break;
    Base := Base + Dir1;
  end;

  Result := Base;
end;

上一页1234下一页

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号