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. (Normally it should be enabled by default.)
One of your files is not downloading?
I noticed on very rare occasions that some files are refusing to download but I'm not too sure what is causing this. Bitsadmin is using its own User-Agent so it's possible that some servers are blocking it. ("User-Agent: Microsoft BITS/7.5") You should be able to use a custom User-Agent by using the command below but I didn't really had time to test it. If you have some information about why some files are refusing to download please leave a comment!
Command for using a custom User-Agent (Is it working?):
"bitsadmin /setcustomheaders mydownloadjob User-Agent: Mozilla/5.0 (X11; Linux i686; rv:124.0) Gecko/20100101 Firefox/124.0"
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 THIS IS YOUR MAIN FUNCTION FOR DOWNLOADING FILES: bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2 GOTO :EOF :DOWNLOAD_PROXY_ON rem THIS FUNCTION IS FOR USING A PROXY SERVER: bitsadmin /setproxysettings mydownloadjob OVERRIDE %1 GOTO :EOF :DOWNLOAD_PROXY_OFF rem THIS FUNCTION DISABLES THE 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 11. :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 WANT 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 THIS WILL DISABLE THE PROXY SERVER IF YOU WERE USING ONE: 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 THIS IS YOUR MAIN FUNCTION FOR DOWNLOADING FILES: bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2 GOTO :EOF :DOWNLOAD_PROXY_ON rem THIS FUNCTION IS FOR USING A PROXY SERVER: bitsadmin /setproxysettings mydownloadjob OVERRIDE %1 GOTO :EOF :DOWNLOAD_PROXY_OFF rem THIS FUNCTION DISABLES THE PROXY SERVER: bitsadmin /setproxysettings mydownloadjob NO_PROXY GOTO :EOF