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. Getting the directory from a path
QtWS25 Last Chance

Getting the directory from a path

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 17.2k Views
  • 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.
  • F Offline
    F Offline
    fcarney
    wrote on 19 Feb 2019, 17:25 last edited by
    #1

    I have a path like this:

    C:/ProgramData/XM Editor/RnD/data/2018-07-01/defaultprog 2018-07-01 14h 53m.csv
    

    I only want this:

    C:/ProgramData/XM Editor/RnD/data/2018-07-01
    

    I can get the filename on its own, but I cannot find a simple way to get the directory name only. I don't want to use string processing that is not cross platform. So far I have looked at these objects:

    QDir
    QUrl
    QFileInfo
    

    I have tried numerous functions and they all return either just the filename or the entire path including the filename. There has to be a simple way to do this that I am overlooking.

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 19 Feb 2019, 17:29 last edited by
      #2

      Hi
      What about
      https://doc.qt.io/qt-5/qfileinfo.html#absolutePath
      "Returns a file's path absolute path. This doesn't include the file name."

      1 Reply Last reply
      4
      • F Offline
        F Offline
        fcarney
        wrote on 19 Feb 2019, 18:23 last edited by
        #3

        Well this is kinda hackish:

            QString csv_filename; // set somewhere else
            QString basename = QUrl(csv_filename).fileName();
            QString basepath = csv_filename.remove(basename);
        

        This seems to work and I need both parts.

        C++ is a perfectly valid school of magic.

        J 1 Reply Last reply 19 Feb 2019, 18:34
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 19 Feb 2019, 18:32 last edited by
          #4

          Hi

           QFileInfo fi("e:/folder name/file name with space.txt");
           qDebug() << fi.absolutePath() << " fn=" << fi.fileName();
          

          gives
          "E:/folder name" fn= "file name with space.txt"

          Did i miss something ?

          F 1 Reply Last reply 19 Feb 2019, 20:57
          3
          • F fcarney
            19 Feb 2019, 18:23

            Well this is kinda hackish:

                QString csv_filename; // set somewhere else
                QString basename = QUrl(csv_filename).fileName();
                QString basepath = csv_filename.remove(basename);
            

            This seems to work and I need both parts.

            J Online
            J Online
            JonB
            wrote on 19 Feb 2019, 18:34 last edited by JonB
            #5

            @fcarney
            What happens if your basename occurs not only at the end but also somewhere in the middle?

            /something/basename/somethingelse/basename
            

            What is wrong with the QFileInfo methods @mrjj suggested which do just what you want? [He's just posted exactly what you need.]

            1 Reply Last reply
            4
            • M mrjj
              19 Feb 2019, 18:32

              Hi

               QFileInfo fi("e:/folder name/file name with space.txt");
               qDebug() << fi.absolutePath() << " fn=" << fi.fileName();
              

              gives
              "E:/folder name" fn= "file name with space.txt"

              Did i miss something ?

              F Offline
              F Offline
              fcarney
              wrote on 19 Feb 2019, 20:57 last edited by
              #6

              @mrjj said in Getting the directory from a path:

              Hi

               QFileInfo fi("e:/folder name/file name with space.txt");
               qDebug() << fi.absolutePath() << " fn=" << fi.fileName();
              

              gives
              "E:/folder name" fn= "file name with space.txt"

              Did i miss something ?

              Heh, no you didnt miss anything. I just posted at nearly the same time as you so I didnt see the update to the thread. Thanks

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              1

              6/6

              19 Feb 2019, 20:57

              • Login

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