Search code examples
batch-fileexewget7zipsourceforge

Wget sourceforge latest 7zip batch


I want to get the latest version of 64bit and 32bit 7zip using wget. However, if I use wget it doesn't download the exe files from the folder, instead it downloads only a file called "7z1507-extra.7z". How can I only get the .EXE files?

The command I invoked:

wget "http://sourceforge.net/projects/sevenzip/files/latest/download" - log.txt --trust-server-name

Log file:

--2015-09-25 21:48:51--  
http://sourceforge.net/projects/sevenzip/files/latest/download
Resolving sourceforge.net (sourceforge.net)... 216.34.181.60
Connecting to sourceforge.net (sourceforge.net)|216.34.181.60|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://downloads.sourceforge.net/project/sevenzip/7-Zip/15.07/7z1507-extra.7z?r=&ts=1443210538&use_mirror=skylink [following]
--2015-09-25 21:48:51--  http://downloads.sourceforge.net/project/sevenzip/7-Zip/15.07/7z1507-extra.7z?r=&ts=1443210538&use_mirror=skylink
Resolving downloads.sourceforge.net (downloads.sourceforge.net)... 216.34.181.59
Connecting to downloads.sourceforge.net (downloads.sourceforge.net)|216.34.181.59|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://skylink.dl.sourceforge.net/project/sevenzip/7-Zip/15.07/7z1507-extra.7z [following]
--2015-09-25 21:48:52--  http://skylink.dl.sourceforge.net/project/sevenzip/7-Zip/15.07/7z1507-extra.7z
Resolving skylink.dl.sourceforge.net (skylink.dl.sourceforge.net)... 109.230.212.53
Connecting to skylink.dl.sourceforge.net (skylink.dl.sourceforge.net)|109.230.212.53|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 849832 (830K) [application/x-7z-compressed]
Saving to: '7z1507-extra.7z'

     0K .......... .......... .......... .......... ..........  6%  848K 1s
    50K .......... .......... .......... .......... .......... 12% 1.31M 1s
   100K .......... .......... .......... .......... .......... 18% 2.10M 1s
   150K .......... .......... .......... .......... .......... 24% 1.53M 0s
   200K .......... .......... .......... .......... .......... 30% 4.76M 0s
   250K .......... .......... .......... .......... .......... 36% 2.10M 0s
   300K .......... .......... .......... .......... .......... 42% 5.13M 0s
   350K .......... .......... .......... .......... .......... 48% 4.73M 0s
   400K .......... .......... .......... .......... .......... 54% 4.02M 0s
   450K .......... .......... .......... .......... .......... 60% 3.77M 0s
   500K .......... .......... .......... .......... .......... 66% 3.73M 0s
   550K .......... .......... .......... .......... .......... 72% 3.69M 0s
   600K .......... .......... .......... .......... .......... 78% 3.76M 0s
   650K .......... .......... .......... .......... .......... 84% 3.27M 0s
   700K .......... .......... .......... .......... .......... 90% 4.05M 0s
   750K .......... .......... .......... .......... .......... 96% 3.85M 0s
   800K .......... .......... .........                       100% 3.88M=0.3s

2015-09-25 21:48:52 (2.56 MB/s) - '7z1507-extra.7z' saved [849832/849832]

To see which files I actually want to get: http://sourceforge.net/projects/sevenzip/files/7-Zip/15.07/

INFO: I specify latest instead of for example 15.07 because I always want the latest version.

Need more info, just ask it!


Solution

  • SourceForge returns the wrong HTTP redirect url when accessed not from a browser, it's their fault, you may file a bug report on their support forum or something.

    However it's still possible to download the page source, grab the correct url and download it:

    @echo off
    echo Retrieving correct download url...
    for /f "delims=;? tokens=2" %%a in (
        'wget http://sourceforge.net/projects/sevenzip/files/latest/download ^
            -q -O - --user-agent="Mozilla/5.0 (Windows NT 6.0)" ^
        ^| find "meta http-equiv=""refresh"""'
    ) do (
        for /f "delims== tokens=1*" %%b in ("%%a") do (
            wget "%%c"
        )
    )
    pause
    

    Explanation:

    1. --user-agent="Mozilla/5.0 (Windows NT 6.0)" is the minimal user-agent string recognized by sourceforge site that makes it serve us an html page as we see it in the browser.
    2. -q silences progress reporting, -O - prints the downloaded page to the standard output pipe
    3. find reads from that pipe and outputs the line with the correct redirect url:
        <meta http-equiv="refresh" content="5; url=http://downloads.sourceforge.net/project/sevenzip/7-Zip/15.07/7z1507.exe?r=&amp;ts=1443257723&amp;use_mirror=netcologne">
    
    1. for /f "delims=;? tokens=2" parses the line above, splitting at ; and ? and extracts the second token: url=http://downloads.sourceforge.net/project/sevenzip/7-Zip/15.07/7z1507.exe
    2. for /f "delims== tokens=1*", likewise, splits at = and grabs everything after it with *
    3. and finally the correct url is used to download the file