Get the directory of a file in a URL.



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


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

    You're welcome !

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



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


  • Lifetime Qt Champion

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

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

    ?



  • [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;
    

    }@


Log in to reply
 

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