[SOLVED] Windows .bat script for downloading zip file that will eventually have a different URL

Jan 27, 2021
42
3
35
0
Hello!

I have a batch script I am working on to automatically update (download new files) for a official Minecraft Bedrock server. I plan to have it compare the checksum of the downloaded .zip to the current .zip file. At the moment, it can download from a URL. However, with future updates, this URL will change with the version number (bedrock-server-1.16.220.02.zip). How can I prevent this? The page that contains the download link is here: https://www.minecraft.net/en-us/download/server/bedrock (the one that contains the URL below).

Code:
bitsadmin /transfer mcpe /download /priority high "https://minecraft.azureedge.net/bin-win/bedrock-server-1.16.220.02.zip" C:\Users\%username%\Documents\bedrockserver\temp\%currentdate%.zip
 
Last edited:

gardenman

Admirable
Moderator
I agree with Mandark that such things usually should be left up to a script, and not a batch file. rgd1101 also mentioned VBS. I would have probably used AutoHotKey.

I worked a little more on it and I'm now happy with it. This wouldn't have been possible without the command rgd1101 provided above. It's all done in a batch file, but as you can tell, it calls up powershell a lot. It probably would have been better to do it all in a powershell script.

The batch does the following:
#1. Downloads the webpage where the link is.
#2. Finds the address to the .ZIP from that webpage.
#3. Creates two command prompt variables with the full address to the download, and just the filename.
#4. See's if the file already exists. If it does, it quits. If it does not, it downloads it and at that point you can unzip it or do whatever you need to do with it.
@Echo off
REM Batch to download new Bedrock server when it exists.
REM Created by multiple people from the Tom's Hardware forums.
REM Thread: https://forums.tomshardware.com/threads/windows-bat-script-for-downloading-zip-file-that-will-eventually-have-a-different-url.3698509/

REM Download the webpage to "temp.html"

powershell -Command "Invoke-WebRequest https://www.minecraft.net/en-us/download/server/bedrock -OutFile temp.html"

REM Get the address to the .ZIP in the temp.html and set the answer to a variable called FullDL
for /f "delims=" %%a in ('powershell -Command "select-string 'https://minecraft.azureedge.net/bin-win/[^""]*' temp.html | foreach-object { $_.Matches[0].Groups[0].Value }"') do set "FullDL=%%a"

REM Get the filename only in the temp.html and set the answer to a variable called FullFN
for /f "delims=" %%a in ('powershell -Command "select-string 'bedrock[^""]*' temp.html | foreach-object { $_.Matches[0].Groups[0].Value }"') do set "FullFN=%%a"

REM Now we have 2 variables, both from the webpage:
REM FullDL=https://minecraft.azureedge.net/bin-win/bedrock-server-1.16.220.02.zip
REM FullFN=bedrock-server-1.16.220.02.zip
REM Now you can do simple things like see if a file exists, or download it.


if exist %FullFN% (
echo File already exists, ignoring.
goto TheEND
) else (
echo File is new, downloading...
powershell -Command "Invoke-WebRequest -Uri %FullDL% -OutFile %FullFN%"
goto TheEND
)

:TheEND


REM Clean up
if exist temp.html del temp.html

pause
This should work as long the host doesn't change addresses of the downloads too much. This will work with updates (different filenames). It does not (and cannot) compare the checksum of the download before downloading it. It just download the file if the same filename doesn't already exist. That's about as close as I can get it and as far as I'm willing to take it.
 

USAFRet

Titan
Moderator
Mar 16, 2013
139,801
7,557
173,940
21,477
  1. To download, how will you bypass the checkbox agreeing to the EULA?
  2. How will you know what the new version number is?
  3. Have you asked the publisher to just put you on their list to get the newest when they update?
 
Jan 27, 2021
42
3
35
0
  1. To download, how will you bypass the checkbox agreeing to the EULA?
  2. How will you know what the new version number is?
  3. Have you asked the publisher to just put you on their list to get the newest when they update?
1. Firstly, that checkbox is just a script on the website. If you go to this link, you can download it without agreeing:
https://minecraft.azureedge.net/bin-win/bedrock-server-1.16.220.02.zip. This is the link it currently takes you to after agreeing to the EULA.

2. I see a script on Linux that downloads and configures the server. The code that does that part is below:

Bash:
# Retrieve latest version of Minecraft Bedrock dedicated server
echo "Checking for the latest version of Minecraft Bedrock server..."
wget -O downloads/version.html https://minecraft.net/en-us/download/server/bedrock/
DownloadURL=$(grep -o 'https://minecraft.azureedge.net/bin-linux/[^"]*' downloads/version.html)
DownloadFile=$(echo "$DownloadURL" | sed 's#.*/##')
echo "$DownloadURL"
echo "$DownloadFile"
The part of this URL that I think is important is the [^"]* part - it somehow substitutes the version into it. This script also downloads the webpage and (I think) somehow determines if there is a new version by checking the hyperlinks. I don't know how this could be accomplished in a bat script though.

3. This software is distributed by Microsoft and only through the previously mentioned website. They don't give notifications. Even if they did, that would not be automating this task.
 

gardenman

Admirable
Moderator
I've been watching this thread and experimenting with different commands. So far I've been able to download the webpage where the link is, and find the line where the link is. Right now I'm having trouble getting just the URL from that line with findstr. I've tried several different things, but here is what I have so far:

@Echo off
powershell -Command "Invoke-WebRequest https://www.minecraft.net/en-us/download/server/bedrock -OutFile temp.html"
findstr "bin-win" temp.html
pause
del temp.html


If you run it, it will display the line of HTML from the webpage that has the URL on it for the download. This line will likely always have "bin-win" in the string. The Linux download has "bin-linux".

So the output is currently:
Code:
<a href="https://minecraft.azureedge.net/bin-win/bedrock-server-1.16.220.02.zip" class="btn btn-disabled-outline mt-4 downloadlink" role="button" data-platform="serverBedrockWindows" tabindex="-1">Download </a>
Now if someone can just figure out how get the URL and only the URL from that line, then you would be set.

I feel this is a good start.
 
Last edited:

gardenman

Admirable
Moderator
I agree with Mandark that such things usually should be left up to a script, and not a batch file. rgd1101 also mentioned VBS. I would have probably used AutoHotKey.

I worked a little more on it and I'm now happy with it. This wouldn't have been possible without the command rgd1101 provided above. It's all done in a batch file, but as you can tell, it calls up powershell a lot. It probably would have been better to do it all in a powershell script.

The batch does the following:
#1. Downloads the webpage where the link is.
#2. Finds the address to the .ZIP from that webpage.
#3. Creates two command prompt variables with the full address to the download, and just the filename.
#4. See's if the file already exists. If it does, it quits. If it does not, it downloads it and at that point you can unzip it or do whatever you need to do with it.
@Echo off
REM Batch to download new Bedrock server when it exists.
REM Created by multiple people from the Tom's Hardware forums.
REM Thread: https://forums.tomshardware.com/threads/windows-bat-script-for-downloading-zip-file-that-will-eventually-have-a-different-url.3698509/

REM Download the webpage to "temp.html"

powershell -Command "Invoke-WebRequest https://www.minecraft.net/en-us/download/server/bedrock -OutFile temp.html"

REM Get the address to the .ZIP in the temp.html and set the answer to a variable called FullDL
for /f "delims=" %%a in ('powershell -Command "select-string 'https://minecraft.azureedge.net/bin-win/[^""]*' temp.html | foreach-object { $_.Matches[0].Groups[0].Value }"') do set "FullDL=%%a"

REM Get the filename only in the temp.html and set the answer to a variable called FullFN
for /f "delims=" %%a in ('powershell -Command "select-string 'bedrock[^""]*' temp.html | foreach-object { $_.Matches[0].Groups[0].Value }"') do set "FullFN=%%a"

REM Now we have 2 variables, both from the webpage:
REM FullDL=https://minecraft.azureedge.net/bin-win/bedrock-server-1.16.220.02.zip
REM FullFN=bedrock-server-1.16.220.02.zip
REM Now you can do simple things like see if a file exists, or download it.


if exist %FullFN% (
echo File already exists, ignoring.
goto TheEND
) else (
echo File is new, downloading...
powershell -Command "Invoke-WebRequest -Uri %FullDL% -OutFile %FullFN%"
goto TheEND
)

:TheEND


REM Clean up
if exist temp.html del temp.html

pause
This should work as long the host doesn't change addresses of the downloads too much. This will work with updates (different filenames). It does not (and cannot) compare the checksum of the download before downloading it. It just download the file if the same filename doesn't already exist. That's about as close as I can get it and as far as I'm willing to take it.
 
Jan 27, 2021
42
3
35
0
Sorry for the delayed response. I haven’t been online for a little while due to being super busy. I want to thank everyone for all the help, but especially @gardenman. Thanks for putting that script together. I certainly could not have done it. :)
 
Last edited:

ASK THE COMMUNITY

TRENDING THREADS