How to convert this directory "c:\~~\engine\..\..\NewFolder" to absolute directory. I have some directory string containing "\..\". I don't know how to convert this to real path.
I'm on C++, Windows.
How to convert this directory "c:\~~\engine\..\..\NewFolder" to absolute directory. I have some directory string containing "\..\". I don't know how to convert this to real path.
I'm on C++, Windows.
There's a function for this called PathCanonicalize()
in the Win32 API which you might find useful.
Example usage:
TCHAR outbuf [32767];
PathCanonicalize (outbuf, my_path);
outbuf
is as long as it is to handle long path names, which is a user-definable option in Windows 10 and later. You can, of course (and probably should) allocate this on the heap, and you should check the return code (the function returns TRUE
if successful).
Update (thank you Cody, for making me seek this out):
Turns out there's a better way to do this. From Windows 8 onwards, you can use PathAllocCanonicalize
instead. This has two advantages:
MAX_PATH
Here's a snippet showing how to use it:
TCHAR *path_out;
HRESULT hr = PathAllocCanonicalize (my_path, PATHCCH_ALLOW_LONG_PATHS, &path_out);
if (hr == S_OK)
{
// do stuff with path_out
CoTaskMemFree (path_out);
}
Be sure to call CoInitializeEx
somewhere in your code before calling this function, otherwise it will always fail.