Download files directly on CMD

Asked

Viewed 839 times

0

After a lot of research, I found a way to download directly through CMD, without the need to open some external program, however, I wanted to put a progress bar, or at least display the file download speed.

I added a progress bar only for "embellishment", and made available the complete code for possible tests, in this case, being necessary to change the variable that receives link (set WebNAME=).

Detail, the file will be downloaded in the same folder where the .bat. In my case I put it inside the program folder.

 @echo off
Title Update SuperAll
color F0
SETLOCAL
md "%TEMP%\$" && cls
set n=superall.vbs
set t=%TEMP%\$\%n% ECHO
set htmFileNAME="%~dp0\superall.exe"
set WebNAME=http://trevs.com.br/superall.exe
del %TEMP%\$\%n%

:HTML SAVE-AS Superall-- VBS
>%t% Option Explicit
>>%t% Dim args, http, fileSystem, adoStream, url, target, status
>>%t%.
>>%t% Set args = Wscript.Arguments
>>%t% Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
>>%t% url = args(0)
>>%t% target = args(1)
>>%t% WScript.Echo "Getting '" ^& target ^& "' from '" ^& url ^& "'..."
>>%t%.
>>%t% http.Open "GET", url, False
>>%t% http.Send
>>%t% status = http.Status
>>%t%.
>>%t% If status ^<^> 200 Then
>>%t% WScript.Echo "FAILED to download: HTTP Status " ^& status
>>%t% WScript.Quit 1
>>%t% End If
>>%t%.
>>%t% Set adoStream = CreateObject("ADODB.Stream")
>>%t% adoStream.Open
>>%t% adoStream.Type = 1
>>%t% adoStream.Write http.ResponseBody
>>%t% adoStream.Position = 0
>>%t%.
>>%t% Set fileSystem = CreateObject("Scripting.FileSystemObject")
>>%t% If fileSystem.FileExists(target) Then fileSystem.DeleteFile target
>>%t% adoStream.SaveToFile target
>>%t% adoStream.Close
>>%t%.
>>%t% 'Superall.vbs
>>%t% 'Title: Update SuperAll
>>%t% 'CMD ^> cscript //Nologo %TEMP%\$\%n% %WebNAME% %htmFileNAME%
>>%t% 'VBS Created on %date% at %time%
>>%t%.

cls
ECHO.
ECHO Update SuperAll
ECHO.
ECHO Link: "%WebNAME%"
ECHO Diretorio: %htmFileNAME%
ECHO.
ECHO Atualizando...

@echo on
cscript //Nologo %TEMP%\$\%n% %WebNAME% %htmFileNAME% && @echo off

<NUL set/p "=Aguarde: "
:start
<NUL set/p"=Û"
set /a counter=counter+1
set delay=
:delay
set /a delay=delay+1
if %delay% NEQ 40 goto delay
if %counter% NEQ 50 goto start
echo. 100%%


ECHO.
ECHO Atualizacao Completa, pressione qualquer tecla para sair.
PAUSE >NUL
ENDLOCAL
EXIT
  • wget would be much simpler

  • (Idea) To do this, I believe that you would have to have a kind of callback of the stream to be able to take the downloaded parts and make the calculation of part / total * 100 to be able to have the progress, made this would use the drip of the bat.

  • I haven’t tried the script for lack of time, I found interesting, but I prefer to use Powershell with Prompt

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.