How do i search a folder for symlinks?



  • I have this code

    @Q_FOREACH(QString file, QDir(parentFolder).entryList()){
    if(QFileInfo(parentFolder+"/"+file).isSymLink())
    {
    qDebug() << parentFolder+"/"+file << "I am a symlink";
    }
    }@

    It works, but it checks every single file. I saw there is a filter about QDir::NoSymLinks, but that's exactly the opposite to what i want...


  • Lifetime Qt Champion

    Hi,

    One way you could try is to create two lists: one with all entries and one without the symlinks. Then convert both to QSet and subtract the two sets, you should then have a QSet containing only the symlink.

    Hope it helps


  • Moderators

    Yeah but iterating twice over a directory will probably be net loss due to the IO overhead.
    I haven't looked in the sources but knowing the corresponding API in Windows my guess would be that the way you have it is an ok way to do it. These APIs usually just iterate over the whole directory and any filtering Qt does is applied on top of it so you might just as well do it yourself the way you need to.



  • I thought of something, but it is not working
    @if(QDir(parentFolder).entryList().count()!=
    QDir(parentFolder).entryList(QDir::NoSymLinks).count())
    {
    Q_FOREACH(QString file, QDir(parentFolder).entryList()){
    qDebug() << file;
    if(QFileInfo(parentFolder+"/"+file).isSymLink())
    {
    qDebug() << parentFolder+"/"+file << "I am a symlink";
    }
    }
    }@

    At start i thought it was correct but apparently
    @qDebug() << QDir(parentFolder).entryList().count() << QDir(parentFolder).entryList(QDir::NoSymLinks).count();@

    returns me
    7 0
    when the folder has 4 files and one shortcut (2 being the dot things :P )
    but why 0. Need an explanation :D


  • Moderators

    Does the link target exist? If not then you need to pass QDir::System to the filter param as "stated in the docs":http://qt-project.org/doc/qt-5/qdir.html#entryList-2



  • [quote author="Chris Kawa" date="1403269096"]Does the link target exist? If not then you need to pass QDir::System to the filter param as "stated in the docs":http://qt-project.org/doc/qt-5/qdir.html#entryList-2[/quote]

    Of course it exists. How could it show that i have 7 files (which is true)? I have the same result at Ubuntu and Windows.



  • Oh you mean the target link. Yes it exists. And QT sees it as a symlink.

    The whole question is why
    @QDir(parentFolder).entryList(QDir::NoSymLinks).count()@
    is 0

    when
    @QDir(parentFolder).entryList().count() @
    is 7

    EDIT
    @QDir(parentFolder).entryList(QDir::AllEntries | QDir::NoSymLinks).count()@
    returns 6, just like i wanted :)


Log in to reply
 

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