0

I have this path :

c:\dev\myapp\dirA\..\dirB\myfile.txt

how to expand it to :

c:\dev\myapp\dirB\myfile.txt

Is their any Delphi functions to do this or any winapi function ?

zeus
  • 12,173
  • 9
  • 63
  • 184

1 Answers1

10

You can use the SysUtils.ExpandFileName() function.

ExpandFileName('c:\dev\myapp\dirA\..\dirB\myfile.txt');

Result:

c:\dev\myapp\dirB\myfile.txt
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ivan Yuzafatau
  • 449
  • 4
  • 11
  • 1
    On Windows, `ExpandFileName()` simply calls the Win32 [`GetFullPathName()`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew) function to handle all of the normalizing. On other platforms, `ExpandFileName()` actually parses the string and does all the normalizing manually. – Remy Lebeau Oct 21 '22 at 01:41
  • 1
    [FPCs `ExpandFileName()`](https://www.freepascal.org/docs-html/rtl/sysutils/expandfilename.html) also parses manually, even resolving `...` and `~`. WinAPI wise see [How do you remove directory qualifiers to simplify pathname using the Win32 API?](https://stackoverflow.com/q/6599458/4299358), including comments. – AmigoJack Oct 21 '22 at 06:36