Downloading files in PURE BATCH...
Without any JScript, VBScript, Powershell, etc... Only pure Batch!
Some people are saying it's not possible of downloading files with a batch script without using any JScript or VBScript, etc... But they are definitely wrong!
Here is a simple method that seems to work pretty well for downloading files in your batch scripts. It should be working on almost any file's URL. It is even possible to use a proxy server if you need it.
For downloading files, we can use BITSADMIN.EXE from the Windows system. There is no need for downloading/installing anything or using any JScript or VBScript, etc. Bitsadmin.exe is present on most Windows versions, probably from XP to Windows 11.
Enjoy!
USAGE:
You can use the BITSADMIN command directly, like this:
bitsadmin /transfer mydownloadjob /download /priority FOREGROUND "http://example.com/File.zip" "C:\Downloads\File.zip"
Proxy Server:
For connecting using a proxy, use this command before downloading.
bitsadmin /setproxysettings mydownloadjob OVERRIDE "proxy-server.com:8080"
Click this LINK if you want more info about BITSadmin.exe
TROUBLESHOOTING:
If you get this error: "Unable to connect to BITS - 0x80070422"
Make sure the windows service "Background Intelligent Transfer Service (BITS)" is enabled and try again. (It should be enabled by default.)
CUSTOM FUNCTIONS
Call :DOWNLOAD_FILE "URL"
Call :DOWNLOAD_PROXY_ON "SERVER:PORT"
Call :DOWNLOAD_PROXY_OFF
I made these 3 functions for simplifying the bitsadmin commands. It's easier to use and remember. It can be particularly useful if you are using it multiple times in your scripts.
PLEASE NOTE...
Before using these functions, you will first need to copy them from CUSTOM_FUNCTIONS.CMD to the end of your script. There is also a complete example: DOWNLOAD-EXAMPLE.CMD
:DOWNLOAD_FILE "URL"
The main function, will download files from URL.
:DOWNLOAD_PROXY_ON "SERVER:PORT"
(Optional) You can use this function if you need to use a proxy server.
Calling the :DOWNLOAD_PROXY_OFF function will disable the proxy server.
EXAMPLE:
CALL :DOWNLOAD_PROXY_ON "proxy-server.com:8080"
CALL :DOWNLOAD_FILE "http://example.com/File.zip" "C:\Downloads\File.zip"
CALL :DOWNLOAD_PROXY_OFF
CUSTOM_FUNCTIONS.CMD
:DOWNLOAD_FILE
rem BITSADMIN COMMAND FOR DOWNLOADING FILES:
bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2
GOTO :EOF
:DOWNLOAD_PROXY_ON
rem FUNCTION FOR USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob OVERRIDE %1
GOTO :EOF
:DOWNLOAD_PROXY_OFF
rem FUNCTION FOR STOP USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob NO_PROXY
GOTO :EOF
DOWNLOAD-EXAMPLE.CMD
@ECHO OFF
SETLOCAL
rem FOR DOWNLOADING FILES, THIS SCRIPT IS USING THE "BITSADMIN.EXE" SYSTEM FILE.
rem IT IS PRESENT ON MOST WINDOWS VERSION, PROBABLY FROM WINDOWS XP TO WINDOWS 10.
:SETUP
rem URL (5MB TEST FILE):
SET "FILE_URL=http://ipv4.download.thinkbroadband.com/5MB.zip"
rem SAVE IN CUSTOM LOCATION:
rem SET "SAVING_TO=C:\Folder\5MB.zip"
rem SAVE IN THE CURRENT DIRECTORY
SET "SAVING_TO=5MB.zip"
SET "SAVING_TO=%~dp0%SAVING_TO%"
:MAIN
ECHO.
ECHO DOWNLOAD SCRIPT EXAMPLE
ECHO.
ECHO FILE URL: "%FILE_URL%"
ECHO SAVING TO: "%SAVING_TO%"
ECHO.
rem UNCOMENT AND MODIFY THE NEXT LINE IF YOU NEED TO USE A PROXY SERVER:
rem CALL :DOWNLOAD_PROXY_ON "PROXY-SERVER.COM:8080"
rem THE MAIN DOWNLOAD COMMAND:
CALL :DOWNLOAD_FILE "%FILE_URL%" "%SAVING_TO%"
rem UNCOMMENT NEXT LINE FOR DISABLING THE PROXY (IF YOU USED IT):
rem CALL :DOWNLOAD_PROXY_OFF
:RESULT
ECHO.
IF EXIST "%SAVING_TO%" ECHO YOUR FILE HAS BEEN SUCCESSFULLY DOWNLOADED.
IF NOT EXIST "%SAVING_TO%" ECHO ERROR, YOUR FILE COULDN'T BE DOWNLOADED.
ECHO.
:EXIT_SCRIPT
PAUSE
EXIT /B
rem FUNCTIONS SECTION
:DOWNLOAD_FILE
rem BITSADMIN COMMAND FOR DOWNLOADING FILES:
bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2
GOTO :EOF
:DOWNLOAD_PROXY_ON
rem FUNCTION FOR USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob OVERRIDE %1
GOTO :EOF
:DOWNLOAD_PROXY_OFF
rem FUNCTION FOR STOP USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob NO_PROXY
GOTO :EOF