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:
- cURL (the latest win32 build from the cURL haxx download page)
- wget (the latest win32 build from Bart Puype)
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:
- download the HTML from http://www.7-zip.org/download.html (using cURL)
- parse the HTML to select the lines containing downloads.sourceforge.net and sevenzip (this is done by the first for /f trick)
- parse out the filename (tricky, as for /f does not allow do use the ” double quote a delimiter)
- 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 comment