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="">Download</A></TD>

So basically, you have to:

  1. download the HTML from (using cURL)
  2. parse the HTML to select the lines containing 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
  set directory=sevenzip
  for /f "usebackq tokens=4,5,6 delims=/<>" %%i in (`curl`) do (
    if !%%i!==!!   if !%%j!==!%directory%!   call :download %%k
  goto :end
  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
  if exist %1 goto :end

Hope the tricks here help a few of you.


Leave a Reply

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

You are commenting using your 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: