In C++, Windows, I have 2 file paths:
How can I tell if Path2 is a descendant of Path1? Is there a Shell function for that? I have searched the Shell API and could not find any.
Note: I don't want to compare the string myself, unless there is a safe way to make the path comparable (handle short names, relative items, etc...).
You could use PathCommonPrefix to compare the two paths to get a common prefix. Then compare the common prefix to Path1 (your directory).
If the two are equal then Path2 should be a descendant.
Or you maybe able to get away without using an out
buffer and just pass NULL
for the third parameter and check that the return value, which is the count of common prefix characters, is equal to Path1's length.
From the PathCommonPrefix
's doc's example, it doesn't look like PathCommonPrefix
includes trailing directory separators in the 'common' prefix, so you'll have to strip off the trailing sep in Path1 or adjust your comparison of the result accordingly for lack of trailing sep.