Automatically Download Last Release on Github


I wanted to download the latest Hugo version and the website is always a nightmare to browse, so here is a Windows bash script to download it automatically and place the hugo.exe file at the right place.

You can adapt it to work with any Github repository. The important line to modify is this one:

for /f "tokens=1,*" %%A in ('curl -kLs https://api.github.com/repos/USER/REPO/releases/latest ^| find "github" ^| find "HAVE_TO_BE_IN_FILENAME" ^| findstr /V "CANT_BE_IN_FILENAME"') do (

Change the username and repository name. It’s basically in the URL of the repo you want to download
USER/REPO:

Those are filters for selecting the right file in the list of files in the release.
HAVE_TO_BE_IN_FILENAME
CANT_BE_IN_FILENAME

setlocal enabledelayedexpansion

@echo off
set DIR=C:\Program Files\Hugo\


cd /D %TMP%
for /f "tokens=1,*" %%A in ('curl -kLs https://api.github.com/repos/gohugoio/hugo/releases/latest ^| find "github" ^| find "_windows-amd64.zip" ^| findstr /V "extended"') do (
    set filename=%%~nxB

    if exist "%DIR%\!filename!" (
      echo "File already exists, do not download"
      exit /b 0
    ) else (
      curl -kOL %%B
      tar -zxvf %%~nxB hugo.exe
      set filename=!filename:_windows-amd64.zip=!
      set filename=!filename:.=_!.exe
      ren hugo.exe !filename!
      set final="%TMP%\!filename!"
      move !final! "%DIR%"
    )
)

cd /D %DIR%
del hugo.exe
mklink hugo.exe %filename%


if NOT ["%errorlevel%"]==["0"] pause 

If you want to create a shortcut instead of a symbolic link, remove the mklink command and you need to use a vbs script with the following commands. The important lines are sLinkFile and oLink.TargetPath.
This can be useful if you need to put the shortcut in the Start menu as SymLinks can’t be put in there.

set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%DIR%\Name_of_the_shortcut.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "%DIR%\%filename%" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%

cscript /nologo %SCRIPT%
del %SCRIPT%
xcopy /Y "%DIR%\Name_of_the_shortcut.lnk" "%appdata%\Microsoft\Windows\Start Menu\Programs"