Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do i search a folder for symlinks?
Qt 6.11 is out! See what's new in the release blog

How do i search a folder for symlinks?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    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...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Leon
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • L Offline
              L Offline
              Leon
              wrote on last edited by
              #6

              [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.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Leon
                wrote on last edited by
                #7

                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 :)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved