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

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

    Join 1,651 other followers

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:

    <TD class="Item" align="center"><A href="http://downloads.sourceforge.net/sevenzip/7z465.exe">Download</A></TD>

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:

@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

Hope the tricks here help a few of you.

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: