Search code examples
delphipathdelphi-2007

Directory path manipulation in Delphi?


I have the full path name of a given folder for e.g.

c:\foo\bar

Now I would like to reference a file inside c:\foo named baz.txt,

c:\foo\bar\..\baz.txt

I am currently using the .. path operator to go down one level and get the file that I need.

Is there a function that can do path manipulations, for e.g. UpOneLevel(str) -> str ? I know I can write one by splitting the string and removing the last token, but I would rather it be a built-in / library function so I don't get into trouble later if there are for e.g. escaped backslashes.


Solution

  • Use the ExpandFileName function:

    var
      S: string;
    begin
      S := 'c:\foo\bar\..';
      S := ExpandFileName(S);
      ShowMessage(S);
    end;
    

    The message from the above example will show the c:\foo path.