0

I am having problems with downloading files from the internet using a Batch script. I found the following pieces of code - though none of them seem to work

xcopy /E /Y "https://website.com/file.bat"
wget.exe --no-check-certificate https://website.com/file.bat

I am trying to make my batch automatically fetch a file, but I am pretty clueless on how to.

I am open to (explained very simple) vbscripts too. Thanks in advance!

john
  • 613
  • 1
  • 7
  • 25
Albert MN.
  • 713
  • 1
  • 16
  • 33
  • 1
    check this - http://stackoverflow.com/questions/28143160/how-can-i-download-a-file-with-batch-file-without-using-any-external-tools – npocmaka Jun 01 '15 at 10:12
  • 1
    please do some reserch. You will probably have to download and install wget if you are going to use it. – john Jun 01 '15 at 10:17
  • I have done a lot of research, but I dont want to download wget as I want this to work on all pc's :) – Albert MN. Jun 01 '15 at 10:50
  • Batch doesn't provide a command which allows you to download a file via HTTP. If you want to do this without installing any 3rd party tools like wget you will have to use powershell or VBScript. However, if the file you want to load is available via FTP you could use the built-in FTP client. – MichaelS Jun 01 '15 at 10:56
  • @MichaelS - `bitsadmin` is exactly a command for downloading via HTTP.Though is a little bit clumsy. – npocmaka Jun 01 '15 at 11:33

2 Answers2

2

This an example to download a game.swf

Try and get inspired by it :

@echo off
mode con:cols=70 lines=8 & Color 9B
Title -==*==- Batch Downloader file by Hackoo -==*==-
(
echo Option Explicit
echo.
echo Dim Message, result
echo Dim Title, Text1, Text2
echo.
echo Message = "Type the URL of the file to download."        
echo Title = "Download a file from URL by Hackoo"
echo Text1 = "You canceled"
echo.
echo result = InputBox^(Message, Title, "http://www.gametop.com/online-free-games/anti-terror-force-online/game.swf", 900, 900^)
echo.
echo.
echo If result = "" Then
echo    WScript.Echo Text1
echo Else
echo    WScript.Echo result
echo End If
)>"%tmp%\inputbox.vbs"
for /f "tokens=* delims=*" %%a in ('Cscript "%tmp%\inputbox.vbs" //nologo') do (set "a=%%a")
(
echo path = "%A%"
echo pos = InStrRev(path, "/"^) +1
echo Const DownloadDest = "%A%"
echo LocalFile = Mid(path, pos^)
echo Const webUser = "admin"
echo Const webPass = "admin"
echo Const DownloadType = "binary"
echo dim strURL
echo.
echo function getit(^)
echo  dim xmlhttp
echo.
echo  set xmlhttp=createobject("MSXML2.XMLHTTP.3.0"^)
echo  'xmlhttp.SetOption 2, 13056 'If https -^) Ignorer toutes les erreurs SSL
echo  strURL = DownloadDest
echo  Wscript.Echo "Download-URL: " ^& strURL
echo.
echo  'Pour l'authentification de base, utilisez la liste ci-dessous, ainsi que les variables + d'utilisateurs? laisser passer
echo  'xmlhttp.Open "GET", strURL, false, WebUser, WebPass
echo  xmlhttp.Open "GET", strURL, false
echo.
echo  xmlhttp.Send
echo  Wscript.Echo "Download-Status: " ^& xmlhttp.Status ^& " " ^& xmlhttp.statusText
echo.
echo  If xmlhttp.Status = 200 Then
echo    Dim objStream
echo    set objStream = CreateObject("ADODB.Stream"^)
echo    objStream.Type = 1 'adTypeBinary
echo    objStream.Open
echo    objStream.Write xmlhttp.responseBody
echo    objStream.SaveToFile LocalFile,2
echo    objStream.Close
echo    set objStream = Nothing
echo  End If
echo.
echo.
echo  set xmlhttp=Nothing
echo End function
echo.
echo '=======================================================================
echo ' Fin Defs de fonction, Start Page
echo '=======================================================================
echo getit(^)
echo Wscript.Echo "Download Completed. Check " ^& LocalFile ^& " for success."
echo Wscript.Quit(intOK^)
)>"%tmp%\httpdownload.vbs"
::Debut
echo Please wait ... The downloading file is in progress ...
echo.
for /f "tokens=* delims=*" %%a in ('Cscript "%tmp%\httpdownload.vbs" //nologo') do (echo "%%a")
Del %tmp%\httpdownload.vbs
::fin
pause>nul
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • 1
    Thank you! I found this *very* usefull. Took out the "httpdownload.vbs". – Albert MN. Jun 01 '15 at 12:36
  • Can I ask you a little question...? When I *just* take the httpdownloader part, it seems to work all fine... *for* a few minutes until the batch won't work again. Is some of the Batch code ^ needed to make it work? – Albert MN. Jun 01 '15 at 13:26
0

If you have PowerShell >= 3.0, you can use Invoke-WebRequest

Invoke-WebRequest -OutFile file.bat https://website.com/file.bat

Or

wget -outf file.bat --no-check-certificate https://website.com/file.bat
Ali Tavallaie
  • 103
  • 1
  • 9