Sometimes you encounter a situation where you might have a relative file-path and you need to ensure that it’s not the same as another path you have (possibly another relative path). For example, “c:\text files\abc.txt” might be the same physical file as “..\..\abc.txt”. Luckily you don’t have to roll out your own custom function to verify whether they are both referring to the same physical file. Instead you can use the GetFullPathName (Kernel32.dll) Win32 API function to convert the relative path to the proper physical path of the filename. By the way one important thing to keep in mind is that GetFullPathName does not verify whether the calculated path is valid or whether it even exists.
The ArePathsSame function
bool ArePathsSame(LPCTSTR lpPath1, LPCTSTR lpPath2)
{
bool ret = false;
TCHAR lpBuffer1[MAX_PATH];
LPTSTR lpFname1 = NULL;
TCHAR lpBuffer2[MAX_PATH];
LPTSTR lpFname2 = NULL;
if( (GetFullPathName(lpPath1,MAX_PATH,
lpBuffer1,&lpFname1) !=0)
&& (GetFullPathName(lpPath2,MAX_PATH,
lpBuffer2,&lpFname2) != 0))
{
if(_tcsicmp(lpBuffer1,lpBuffer2) == 0)
ret = true;
}
return ret;
}
Using the ArePathsSame function
SetCurrentDirectory("C:\\nish\\aoe2\\Screenshots");
bool bSame = ArePathsSame(
"C:\\nish\\aoe2\\History\\Armies.txt",
"..\\HISTORY\\Armies.txt");
if(bSame)
cout << "Paths are physically the same\r\n";
*Sigh*
WordPress strips out every occurrence of a slash. Right now am trying to figure out how to fix this 😦
Okay, fixed it for now by replacing every instance of \ with
\Nish
As you have told “GetFullPathName does not verify whether the calculated path is valid or whether it even exists” we can also add an pathFileExits() inside the ArePathsSame()before it returns the status .
Excellent! That helped me, What is GetLongPathName is used for?