Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi AbsoluterPfad zu Relativen Pfad (https://www.delphipraxis.net/156362-absoluterpfad-zu-relativen-pfad.html)

Thomas Feichtner 29. Nov 2010 16:00

AbsoluterPfad zu Relativen Pfad
 
Hallo, ich muss einen absoluten Pfad in einen relativen umwandeln. Ich habe nichts nützliches gefunden. :-(

Daher habe ich mir selbst eine Funktion gebaut:
Habe ich auf etwas vergessen?
Oder geht das ganze einfacher?

Delphi-Quellcode:
function GetRelativeFilename(pCurrentDirectory, pAbsoluteFilename: String): String;
var
  iLen1, iLen2, iMinLen, iCount: Integer;
  sResult, sPfad1, sPfad2, sTemp1, sFile: string;
begin
  Result := pAbsoluteFilename;
  sPfad1 := AnsiLowerCase(ExtractFilePath(pCurrentDirectory));
  sPfad2 := AnsiLowerCase(ExtractFilePath(pAbsoluteFilename));
  sFile := AnsiLowerCase(ExtractFileName(pAbsoluteFilename));
  { Bin ich im gleichen Verzeichnis }
  if Copy(sPfad1, 1, 3) <> Copy(sPfad2, 1, 3) then
    Exit;

  { Zuerst die gemeinsamen angaben entfernen}
  iLen1 := Length(sPfad1);
  iLen2 := Length(sPfad2);
  iMinLen := iLen1;
  if iLen2 < iMinLen then
    iMinLen := iLen2;
  sTemp1 := '';
  for iCount := 1 to iMinLen do
  begin
    if sPfad1[iCount] = sPfad2[iCount] then
      sTemp1 := sTemp1 + sPfad1[iCount]
    else
      Break;
  end;
  sPfad1 := StringReplace(sPfad1, sTemp1, '', [rfReplaceAll]);
  sPfad2 := StringReplace(sPfad2, sTemp1, '', [rfReplaceAll]);

  sTemp1 := '';
  if sPfad1 <> '' then
  begin
    for iCount := 1 to Length(sPfad1) do
    begin
      if sPfad1[iCount] = '\' then
        sTemp1 := sTemp1 + '..\';
    end;
  end;
  sResult := sTemp1 + sPfad2 + sFile;
  Result := sResult;
end;

shmia 29. Nov 2010 16:13

AW: AbsoluterPfad zu Relativen Pfad
 
Nimm doch
Delphi-Quellcode:
function ExtractRelativePath(const BaseName, DestName: string): string;
aus Unit SysUtils.

Thomas Feichtner 30. Nov 2010 06:54

AW: AbsoluterPfad zu Relativen Pfad
 
Danke,

dann kann ich meines wieder verwerfen. Ich hatte es einfach nicht gefunden. :-(


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:36 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz