user5153901
user5153901

Reputation:

How to compare downloaded content in Batch

So, recently, I was designing an update checker that will download and execute an update script if version.txt on the web is not equal to a local string. I have already made this in bash using cURL and an if statement.

# The bash implementation
echo -n "Checking for updates..."
up=$(curl https://raw.githubusercontent.com/aaronliu0130/trashBin/master/version)
if [[$up!="v0.2.0-alpha"]]; then
    clear
    echo "An update was found. Now updating. Will restart after update."
    curl -o update.sh https://raw.githubusercontent.com/aaronliu0130/trashBin/master/update.sh
    bash update.sh
    exit
fi

Now I'm trying to implement this in windows batch. However, I can't find a way to download a file to a variable like cURL or compare a file's contents with a magic string. How do I do what the above code does in batch?

Upvotes: 1

Views: 158

Answers (1)

npocmaka
npocmaka

Reputation: 57272

try this:

@echo off
del  /q version.txt >nul 2>&1
bitsadmin /transfer myDownloadJob /download /priority normal https://raw.githubusercontent.com/aaronliu0130/trashBin/master/version %cd%\version.txt

for /f "tokens=* delims=" %%v in (version.txt) do set "version=%%v"

if "%version%" NEQ "v0.2.0-alpha" (
    echo update has been found.
    bitsadmin /transfer myDownloadJob /download /priority normal https://raw.githubusercontent.com/aaronliu0130/trashBin/master/update.sh %cd%\update.sh
    rem if you have linux subsystems for windows installed you can run the line bellow
    rem bash update.sh
)

You cant directly load to memory a downloaded content so you'll have to rely on temp files.

Upvotes: 1

Related Questions