PDA

View Full Version : Count number of directory nesting



franco.amato
1st October 2010, 18:36
Hi to all.
I would count the number of directory nesting between a main directory and another.
For example:
main_directory = C:/IrisDatabase
directory = C:/IrisDatabase/1/session1/

What I would implement is a function doing this: nestingDirectory( main_directory, directory ) and it should return 2 ( the number of nested directories under the main directory.

I hope my question is clear as my english is not so good.

Best Regards

SixDegrees
1st October 2010, 18:42
Count slashes.

Lykurg
1st October 2010, 18:43
So, where exactly is the problem. Use QDir and use QDir::cdUp() till both are the same. You need of course to count how often you cd up. Or simply split on / and subtract one from the other.

Urthas
1st October 2010, 20:04
My first thought too was to count slashes, but you have to be careful about terminal slashes (that is, "/" is the last character in the string).

Probably the best thing to do is strip or otherwise take into account any terminal slashes before doing your counting. It's not a problem if your root and target directories both end in "/" or both don't end in "/" but you'd get the wrong answer if one ended in "/" and the other didn't, as in your example.

squidge
1st October 2010, 20:07
So run both strings through QDir::cleanPath before counting the slashes :)

franco.amato
1st October 2010, 21:08
Thank to all