Einzelnen Beitrag anzeigen

Benutzerbild von SirThornberry
SirThornberry
(Moderator)

Registriert seit: 23. Sep 2003
Ort: Bockwen
12.235 Beiträge
 
Delphi 2006 Professional
 
#9

Re: relativen -> absoluten Pfad (als Text) erzeugen

  Alt 19. Apr 2007, 13:42
ich hab mir dafür mal folgende Funktion geschrieben:
Delphi-Quellcode:
function RelToAbsPath(ACurPath, ARelPath: String): String;
var
  lCopiedTo,
  lCount : Integer;
  lParts : TStringList;
  lStr : String;
begin
  ARelPath := StringReplace(ARelPath, '/', '\', [rfReplaceAll]);
  ACurPath := StringReplace(ACurPath, '/', '\', [rfReplaceAll]);
  lParts := TStringList.Create();

  if (copy(ARelPath, 1, 2) = '.\') or (copy(ARelPath, 1, 3) = '..\') then
  begin
    if (copy(ACurPath, Length(ACurPath), 1) = '\') then
      ARelPath := ACurPath + ARelPath
    else
      ARelPath := ACurPath + '\' + ARelPath;
  end;

  lCopiedto := 0;
  for lCount := 1 to Length(ARelPath) - 1 do
  begin
    if (ARelPath[lCount] = '\') then
    begin
      lStr := copy(ARelPath, lCopiedto + 1, lCount - lCopiedTo - 1);
      if (lStr <> '') then
        lParts.Add(lStr);
      lCopiedTo := lCount;
    end;
  end;
  lStr := copy(ARelPath, lCopiedTo + 1, Length(ARelPath));
  if (lStr <> '') then
    lParts.Add(lStr);

  lCount := 0;
  while (lCount < lParts.Count) do
  begin
    if (lParts.Strings[lCount] = '.') then
      lParts.Delete(lCount)
    else if (lParts.Strings[lCount] = '..') then
    begin
      lParts.Delete(lCount);
      if (lCount > 1) then
      begin
        lParts.Delete(lCount - 1);
        dec(lCount);
      end;
    end
    else
      inc(lCount);
  end;

  lStr := '';
  for lCount := 0 to lParts.Count - 2 do
    lStr := lStr + lParts.Strings[lCount] + '\';
  if (lParts.Count > 0) then
    lStr := lStr + lParts.Strings[lParts.Count - 1];

  lParts.Free;

  result := lStr;
end;
Jens
Mit Source ist es wie mit Kunst - Hauptsache der Künstler versteht's
  Mit Zitat antworten Zitat