Results 1 to 6 of 6

Thread: Count number of directory nesting

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Count number of directory nesting

    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
    Franco Amato

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Count number of directory nesting

    Count slashes.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Count number of directory nesting

    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.

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Count number of directory nesting

    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.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Count number of directory nesting

    So run both strings through QDir::cleanPath before counting the slashes

  6. #6
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Count number of directory nesting

    Thank to all
    Franco Amato

Similar Threads

  1. Count files of a directory
    By radu_d in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2012, 03:17
  2. Count the number of rows in a QTableView
    By grub87 in forum Qt Programming
    Replies: 9
    Last Post: 28th June 2009, 16:31
  3. Dock Nesting Problem
    By blaylockr in forum Newbie
    Replies: 4
    Last Post: 14th August 2008, 13:59
  4. QDockWidget nesting problem..
    By aamer4yu in forum Qt Programming
    Replies: 6
    Last Post: 31st January 2007, 12:23
  5. count line number in text
    By Alina in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2006, 08:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.