Remove the path of a QString file



  • Imagine we have QString file. File has directories any time. For example:

    C:/Qt/5.6/mingw49_32/bin/example.xml
    C:/Qt/raining/now/clouds/nature.png
    D:/sun/no_clouds/newworldorder/soon.txt

    Is there a smart way to take the names from this file:

    example.xml
    nature.png
    soon.txt

    or the last two names:

    bin/example.xml
    clouds/nature.png
    newworldorder/soon.txt

    There is the choice .remove.

    But you have to know what you want to remove, for example, file.remove("C:/Qt/5.6/mingw49_32/bin/").

    If you only want to remove all the folder - file names, except the last two ones?

    removefolder(2, file)?

    Thanks in advance.


  • Qt Champions 2016

    @Konstantinos
    Hi,
    Use the specialized API in QFile, QDir and QFileInfo. For example:

    const QString filePath("C:/Qt/5.6/mingw49_32/bin/example.xml");
    
    QFileInfo info(filePath);
    qDebug<< info.fileName() << endl << info.dir().path();
    

    Would output:

    example.xml
    C:/Qt/5.6/mingw49_32/bin
    

    Kind regards.



  • @kshegunov

    Thank you for your help.


Log in to reply
 

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