// This solution is intended for use on Windows platforms.
// If paths may be provided in relative format, use PathCchCanonicalize to convert to absolute.
// If unsure, use PathIsRelative to verify and convert as needed.
// If paths may be provided with file specs included, use PathIsFileSpec to identify,
// and then PathCchRemoveFileSpec to remove it.
TCHAR tszPathSource[] = TEXT("c:\\x\\abc\\123");
TCHAR tszPathTarget[] = TEXT("c:\\x\\abc\\456");
LPTSTR lptszPathSource = &tszPathSource[0];
LPTSTR lptszPathTarget = &tszPathTarget[0];
// Verify that the source and target exist before going forward.
// The source may not seem as important, but if it's invalid... it's likely a bug.
if (PathIsDirectory(lptszPathSource) && PathIsDirectory(lptszPathTarget))
{
// Advance the pointers to the point that the two paths converge.
while (*lptszPathSource && *lptszPathTarget)
{
if (*lptszPathSource != *lptszPathTarget)
{
// When they no longer converge, assign the remaining parts of the source to the target.
while (*lptszPathSource)
{
*lptszPathTarget = *lptszPathSource;
lptszPathSource++;
lptszPathTarget++;
}
// Change to the target path.
SetCurrentDirectory(tszPathTarget);
break;
}
lptszPathSource++;
lptszPathTarget++;
}
}