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. Get the directory of a file in a URL.
QtWS25 Last Chance

Get the directory of a file in a URL.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.7k 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.
  • X Offline
    X Offline
    xerxef
    wrote on last edited by
    #1

    Hi,
    I'm using the downloadmanager example from Qt Creator. I added two URLs to the download queue, "http://example.com/bleh/blah/DIR/file.exe" and ""http://example.com/bleh/blah/file2.exe". I want to know if there's a way that if the files are in "DIR" (Like file2.exe), the application creates that directory in where the files are downloaded and save it there.

    this is how the app takes the filename:

    @QString FileDownloader::saveFileName(const QUrl &url)
    {
    QString path = url.path();
    QString basename = QFileInfo(path).fileName();

    if (QFile::exists(basename)) {
        // already exists, overwrite
        QFile::remove(basename);
    }
    
    return basename;
    

    }@

    I'm new on Qt dev btw, hope someone can help me.

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

      Hi and welcome to devnet,

      Since you know the urls beforehand, you can parse them for the additional path element you have, create the missing folder and update the output file to point there.

      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
      • X Offline
        X Offline
        xerxef
        wrote on last edited by
        #3

        [quote author="SGaist" date="1418937782"]Hi and welcome to devnet,

        Since you know the urls beforehand, you can parse them for the additional path element you have, create the missing folder and update the output file to point there.[/quote]

        Didn't think on that before, actually, that was a very good suggestion.
        I did it like this

        @if(path.contains("/dir", Qt::CaseInsensitive))
        {
        if(!QDir("dir").exists())
        QDir().mkdir("dir");
        basename = basename.prepend("dir/");
        }@

        Thanks.

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

          You're welcome !

          Are you sure that hardcoding "/dir" is the right choice ? What if you have another path ?

          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
          • X Offline
            X Offline
            xerxef
            wrote on last edited by
            #5

            [quote author="SGaist" date="1418949075"]You're welcome !

            Are you sure that hardcoding "/dir" is the right choice ? What if you have another path ?[/quote]

            Well, actually it does have another path and not just "/dir" only.

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

              In your use case, do you always have something like:

              @“http://example.com/bleh/blah/plus_something_else/myBar.exe"@

              ?

              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
              • X Offline
                X Offline
                xerxef
                wrote on last edited by
                #7

                [quote author="SGaist" date="1419033756"]In your use case, do you always have something like:

                @“http://example.com/bleh/blah/plus_something_else/myBar.exe"@

                ?[/quote]

                Nope. Some files are on "http://example.com/bleh/blah", others in "http://example.com/bleh/blah/something_else" and "something_else2".
                For now, since I know what the name of the 3 different subfolders are, I did this:

                @QString FileDownloader::saveFileName(const QUrl &url)
                {
                QString path = url.path();
                QString basename;
                QString finalPath;

                if(path.contains("/SubDir1"))
                    finalPath = "SubDir1/";
                else if(path.contains("/SubDir2"))
                    finalPath = "SubDir2/";
                else if(path.contains("/SubDir3"))
                    finalPath = "SubDir3/";
                
                if(path.contains(finalPath, Qt::CaseInsensitive))
                {
                    if(!QDir(finalPath).exists())
                        QDir().mkdir(finalPath);
                
                    basename = url.fileName().prepend(finalPath);
                }
                else
                {
                    basename = url.fileName();
                }
                
                qDebug() <&lt; basename;
                
                if (QFile::exists(basename)) {
                    // already exists, overwrite
                    QFile::remove(basename);
                }
                
                return basename;
                

                }@

                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