Mega Code Archive

 
Categories / Delphi / Files
 

Simplify a filepath

Title: Simplify a filepath Question: Simplify complex file path (c:\temp\prout\ouch\..\in\..) in smallest absolute path (c:\temp\PROUT). Answer: It is a complex task to make an algorithm to remove '..\' in a pathname, so i let this to the GetFullPathName() API. Here is the function : function SimplifyPath(var path : string) : integer; var lpFileName : pchar; lpBuffer : array[0..MAX_PATH] of char; begin lpFileName := PCHAR(path); result := GetFullPathName(lpFileName, MAX_PATH, lpBuffer, lpFileName); path := lpBuffer; end; Example : uses windows path := 'c:\temp\prout\ouch\..\in\..'; SimplifyPath(path); writeln(path); GetFullPathName() do not check if the path or the file do not exists. It works with UNC (\\machinename\share). Search for GetFullPathName() in WinAPI help for more information. Tested with D3 and NT4