To solve something like this, in addition to the error-checking and informing of cases where ambiguity may exist, I would gravitate toward reverse sorting the directory list by length, then parsing/matching for the longest possible strings first. This would make the directory tree match to its fullest extent possible.
If there are no ambiguities possible, given the nomenclature of the directories and files, then this may be an unnecessary step.