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 FUNCTIONSCall :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_FILErem BITSADMIN COMMAND FOR DOWNLOADING FILES:bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2GOTO :EOF:DOWNLOAD_PROXY_ONrem FUNCTION FOR USING A PROXY SERVER:bitsadmin /setproxysettings mydownloadjob OVERRIDE %1GOTO :EOF:DOWNLOAD_PROXY_OFFrem FUNCTION FOR STOP USING A PROXY SERVER:bitsadmin /setproxysettings mydownloadjob NO_PROXYGOTO :EOF
DOWNLOAD-EXAMPLE.CMD
@ECHO OFFSETLOCALrem 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.:SETUPrem 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 DIRECTORYSET "SAVING_TO=5MB.zip"SET "SAVING_TO=%~dp0%SAVING_TO%":MAINECHO.ECHO DOWNLOAD SCRIPT EXAMPLEECHO.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:RESULTECHO.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_SCRIPTPAUSEEXIT /Brem FUNCTIONS SECTION:DOWNLOAD_FILErem BITSADMIN COMMAND FOR DOWNLOADING FILES:bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2GOTO :EOF:DOWNLOAD_PROXY_ONrem FUNCTION FOR USING A PROXY SERVER:bitsadmin /setproxysettings mydownloadjob OVERRIDE %1GOTO :EOF:DOWNLOAD_PROXY_OFFrem FUNCTION FOR STOP USING A PROXY SERVER:bitsadmin /setproxysettings mydownloadjob NO_PROXYGOTO :EOF
' Create an HTTP objectmyURL = "http://www.google.com"Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )' Download the specified URLobjHTTP.Open "GET", myURL, FalseobjHTTP.SendintStatus = objHTTP.StatusIf intStatus = 200 ThenWScript.Echo " " & intStatus & " A OK " +myURLElseWScript.Echo "OOPS" +myURLEnd If
then
C:\>cscript geturl.vbsMicrosoft (R) Windows Script Host Version 5.7Copyright (C) Microsoft Corporation. All rights reserved.200 A OK http://www.google.com
or just double click it to test in windows
AFAIK, Windows doesn't have a built-in commandline tool to download a file. But you can do it from a VBScript, and you can generate the VBScript file from batch using echo and output redirection:
@echo offrem Windows has no built-in wget or curl, so generate a VBS script to do it:rem -------------------------------------------------------------------------set DLOAD_SCRIPT=download.vbsecho Option Explicit > %DLOAD_SCRIPT%echo Dim args, http, fileSystem, adoStream, url, target, status >> %DLOAD_SCRIPT%echo. >> %DLOAD_SCRIPT%echo Set args = Wscript.Arguments >> %DLOAD_SCRIPT%echo Set http = CreateObject("WinHttp.WinHttpRequest.5.1") >> %DLOAD_SCRIPT%echo url = args(0) >> %DLOAD_SCRIPT%echo target = args(1) >> %DLOAD_SCRIPT%echo WScript.Echo "Getting '" ^& target ^& "' from '" ^& url ^& "'..." >> %DLOAD_SCRIPT%echo. >> %DLOAD_SCRIPT%echo http.Open "GET", url, False >> %DLOAD_SCRIPT%echo http.Send >> %DLOAD_SCRIPT%echo status = http.Status >> %DLOAD_SCRIPT%echo. >> %DLOAD_SCRIPT%echo If status ^<^> 200 Then >> %DLOAD_SCRIPT%echo WScript.Echo "FAILED to download: HTTP Status " ^& status >> %DLOAD_SCRIPT%echo WScript.Quit 1 >> %DLOAD_SCRIPT%echo End If >> %DLOAD_SCRIPT%echo. >> %DLOAD_SCRIPT%echo Set adoStream = CreateObject("ADODB.Stream") >> %DLOAD_SCRIPT%echo adoStream.Open >> %DLOAD_SCRIPT%echo adoStream.Type = 1 >> %DLOAD_SCRIPT%echo adoStream.Write http.ResponseBody >> %DLOAD_SCRIPT%echo adoStream.Position = 0 >> %DLOAD_SCRIPT%echo. >> %DLOAD_SCRIPT%echo Set fileSystem = CreateObject("Scripting.FileSystemObject") >> %DLOAD_SCRIPT%echo If fileSystem.FileExists(target) Then fileSystem.DeleteFile target >> %DLOAD_SCRIPT%echo adoStream.SaveToFile target >> %DLOAD_SCRIPT%echo adoStream.Close >> %DLOAD_SCRIPT%echo. >> %DLOAD_SCRIPT%rem -------------------------------------------------------------------------cscript //Nologo %DLOAD_SCRIPT% http://example.com targetPathAndFile.html
More explanation here
With the build 17063 of windows 10 the CURL utility was added. To download a file you can use:
curl "https://download.sysinternals.com/files/PSTools.zip" --output pstools.zip
It can be easier to use bitsadmin with a macro:
set "download=bitsadmin /transfer myDownloadJob /download /priority normal"%download% "https://download.sysinternals.com/files/PSTools.zip" %cd%\pstools.zip
For backward compatibility you can use winhttpjs.bat (with this you can perform also POST,DELETE and the others http methods):
call winhhtpjs.bat "https://example.com/files/some.zip" -saveTo "c:\somezip.zip"
Download Wget from here http://downloads.sourceforge.net/gnuwin32/wget-1.11.4-1-setup.exe
Then install it.
Then make some .bat file and put this into it
@echo offfor /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set y=%%kfor /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set d=%%k%%i%%jfor /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do set t=%%i%%jset t=%t%_if "%t:~3,1%"=="_" set t=0%t%set t=%t:~0,4%set "theFilename=%d%%t%"echo %theFilename%cd "C:\Program Files\GnuWin32\bin"wget.exe --output-document C:\backup\file_%theFilename%.zip http://someurl/file.zip
Adjust the URL and the file path in the script
You cannot use xcopy over http. Try downloading wget for windows. That may do the trick. It is a command line utility for non-interactive download of files through http. You can get it at http://gnuwin32.sourceforge.net/packages/wget.htm
If bitsadmin isn't your cup of tea, you can use this PowerShell command:
Start-BitsTransfer -Source http://www.foo.com/package.zip -Destination C:\somedir\package.zip
Use Bat To Exe Converter
Create a batch file and put something like the code below into it
%extd% /download http://www.examplesite.com/file.zip file.zip
or
%extd% /download http://stackoverflow.com/questions/4619088/windows-batch-file-file-download-from-a-url thistopic.html
and convert it to exe.
Instead of wget you can also use aria2 to download the file from a particular URL.
See the following link which will explain more about aria2:
https://aria2.github.io/
There's a utility (resides with CMD) on Windows which can be run from CMD (if you have write access):
set url=https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpgset file=file.jpgcertutil -urlcache -split -f %url% %file%echo Done.
Built in Windows application. No need for external downloads.
Tested on Win 10
BATCH may not be able to do this, but you can use JScript or VBScript if you don't want to use tools that are not installed by default with Windows.
The first example on this page downloads a binary file in VBScript:http://www.robvanderwoude.com/vbstech_internet_download.php
This SO answer downloads a file using JScript (IMO, the better language):Windows Script Host (jscript): how do i download a binary file?
Your batch script can then just call out to a JScript or VBScript that downloads the file.
This should work i did the following for a game server project. It will download the zip and extract it to what ever directory you specify.
Save as name.bat or name.cmd
@echo offset downloadurl=http://media.steampowered.com/installer/steamcmd.zipset downloadpath=C:\steamcmd\steamcmd.zipset directory=C:\steamcmd\%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& {Import-Module BitsTransfer;Start-BitsTransfer '%downloadurl%' '%downloadpath%';$shell = new-object -com shell.application;$zip = $shell.NameSpace('%downloadpath%');foreach($item in $zip.items()){$shell.Namespace('%directory%').copyhere($item);};remove-item '%downloadpath%';}"echo download complete and extracted to the directory.pause
Original : https://github.com/C0nw0nk/SteamCMD-AutoUpdate-Any-Gameserver/blob/master/steam.cmd
I found this VB script:
http://www.olafrv.com/?p=385
Works like a charm. Configured as a function with a very simple function call:
SaveWebBinary "http://server/file1.ext1", "C:/file2.ext2"
Originally from: http://www.ericphelps.com/scripting/samples/BinaryDownload/index.htm
Here is the full code for redundancy:
Function SaveWebBinary(strUrl, strFile) 'As BooleanConst adTypeBinary = 1Const adSaveCreateOverWrite = 2Const ForWriting = 2Dim web, varByteArray, strData, strBuffer, lngCounter, adoOn Error Resume Next'Download the file with any available objectErr.ClearSet web = NothingSet web = CreateObject("WinHttp.WinHttpRequest.5.1")If web Is Nothing Then Set web = CreateObject("WinHttp.WinHttpRequest")If web Is Nothing Then Set web = CreateObject("MSXML2.ServerXMLHTTP")If web Is Nothing Then Set web = CreateObject("Microsoft.XMLHTTP")web.Open "GET", strURL, Falseweb.SendIf Err.Number <> 0 ThenSaveWebBinary = FalseSet web = NothingExit FunctionEnd IfIf web.Status <> "200" ThenSaveWebBinary = FalseSet web = NothingExit FunctionEnd IfvarByteArray = web.ResponseBodySet web = Nothing'Now save the file with any available methodOn Error Resume NextSet ado = NothingSet ado = CreateObject("ADODB.Stream")If ado Is Nothing ThenSet fs = CreateObject("Scripting.FileSystemObject")Set ts = fs.OpenTextFile(strFile, ForWriting, True)strData = ""strBuffer = ""For lngCounter = 0 to UBound(varByteArray)ts.Write Chr(255 And Ascb(Midb(varByteArray,lngCounter + 1, 1)))Nextts.CloseElseado.Type = adTypeBinaryado.Openado.Write varByteArrayado.SaveToFile strFile, adSaveCreateOverWriteado.CloseEnd IfSaveWebBinary = TrueEnd Function
This question has very good answer in here. My code is purely based on that answer with some modifications.
Save below snippet as wget.bat and put it in your system path (e.g. Put it in a directory and add this directory to system path.)
You can use it in your cli as follows:
wget url/to/file [?custom_name]
where url_to_file
is compulsory and custom_name
is optional
The file url and saved filenames are displayed in ansi colored text. If that is causing problem for you, then check this github project.
@echo OFFsetLocal EnableDelayedExpansionset Url=%1set Url=!Url:http://=!set Url=!Url:/=,!set Url=!Url:%%20=?!set Url=!Url: =?!call :LOOP !Url!set FileName=%2if "%2"=="" set FileName=!FN!echo.echo.Downloading: [1;33m%1[0m to [1;33m\!FileName![0mpowershell.exe -Command wget %1 -OutFile !FileName!goto :EOF:LOOPif "%1"=="" goto :EOFset FN=%1set FN=!FN:?= !shiftgoto :LOOP
P.S. This code requires you to have PowerShell installed.
You can setup a scheduled task using wget, use the “Run” field in scheduled task as:
C:\wget\wget.exe -q -O nul "http://www.google.com/shedule.me"
use ftp:
(ftp *yourewebsite.com*-a)cd *directory*get *filename.doc*close
Change everything in asterisks to fit your situation.