Check how deep folder is compared to other folder



  • Hello,

    Lets say I have base folder /test
    I wonder if there is something standard in Qt that would let me know how deep I am from base folder.
    I.e. My other folder is /test/sample/end and comparing to base folder it would return 2 then /test/sample would return 1 and /test would return 0.



  • Try:

    int countDepth(QDir directory){
    int result;
    for(result=0;directory.cdUp();++result){}
    return result;
    }
    


  • @VRonin I have tried it and added check to see if folder includes base folder

    QDir basePath("/home/something/test");
    QDir t("/home/something/test/sample/end");
    QDirIterator it(basePath, QDirIterator::Subdirectories);
    
    bool isBase = false;
    
    while (it.hasNext()) {
        if (it.next() == t.path()) {
            isBase = true;
            break;
        }
    }
    
    int i = 0;
    if (isBase) {
        while (basePath.path() != t.path()) {
            t.cdUp();
            i++;
        }
    } else {
        i = - 1;
    }
    
    qDebug() << i;


  • if t is not a subdirectory of basePath this is an infinite loop



  • @VRonin Huh? I'm checking with QDirIterator if t is a subdirectory of basePath and setting isBase then only if isBase is true i'm going into loop. I have tested and it's working as intended or am I missing something here?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.