The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 717 other subscribers

Batch file tricks – double quotes splitting and downloading latest 7-zip

Posted by jpluimers on 2010/09/02

I needed a quick means to download the latest 7-zip from the command-line in Windows.

This batchfile makes use of these tools:

7-zip has a download page that contains lines like these:

[sourcecode language=”html”]
<TD class="Item" align="center"><A href="http://downloads.sourceforge.net/sevenzip/7z465.exe">Download</A></TD&gt;
[/sourcecode]

So basically, you have to:

  1. download the HTML from http://www.7-zip.org/download.html (using cURL)
  2. parse the HTML to select the lines containing downloads.sourceforge.net and sevenzip (this is done by the first for /f trick)
  3. parse out the filename (tricky, as for /f does not allow do use the ” double quote a delimiter)
  4. download the 7zip binary (using wget)

Using the double quote character ” as a delimiter in the for /f statement is not possible.
But luckily, the SET command allows you to substitution, as jumper explained here.
Since we already parsed out <> (we included them as delimiters) you don’t get tricky redirection issues.

The batch-file is this:

[sourcecode language=”text”]
@echo off
setlocal
set directory=sevenzip
for /f "usebackq tokens=4,5,6 delims=/<>" %%i in (`curl http://www.7-zip.org/download.html`) do (
if !%%i!==!downloads.sourceforge.net! if !%%j!==!%directory%! call :download %%k
)
endlocal
goto :end
:download
rem substitute " with !, as " cannot be specified as a delimiter
set file=%1
set file=%file:"=!%
for /f "usebackq tokens=1 delims=!" %%f IN (`call echo.%file%`) do (
call :wget %%f
)
goto :end
:wget
if exist %1 goto :end
wget http://downloads.sourceforge.net/sourceforge/%directory%/%1
:end
[/sourcecode]

Hope the tricks here help a few of you.

–jeroen

Leave a Comment