npocmaka

download.bat

Apr 30th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   @echo off
  2. rem -- download utility by Vasil "npocmaka" Arnaudov
  3.     setlocal
  4.     :download
  5.    
  6.     if "%2" equ "" (
  7.         call :help
  8.         exit /b 5
  9.     )
  10.    
  11.     if "%1" equ "" (
  12.         call :help
  13.         exit /b 6
  14.     )
  15.     set url=%~1
  16.     set file=%~2
  17.     rem ----
  18.     if "%~3" NEQ "" (
  19.         set /A timeout=%~3
  20.     ) else (
  21.         set timeout=5
  22.     )
  23.  
  24.     bitsadmin /cancel download
  25.     bitsadmin /create /download download
  26.     call bitsadmin /addfile download "%url%" "%CD%\%file%"
  27.     bitsadmin /resume download
  28.     butsadmin /setproxysettings download AUTODETECT
  29.  
  30.     set /a attempts=0
  31.     :repeat
  32.     set /a attempts +=1
  33.     if "%attempts%" EQU "10" (
  34.         echo TIMED OUT
  35.         endlocal
  36.         exit /b 1
  37.     )
  38.     bitsadmin /info download /verbose | find  "STATE: ERROR" && endlocal &&  bitsadmin /cancel download && echo SOME KIND OF ERROR && exit /b 2
  39.     bitsadmin /info download /verbose | find  "STATE: SUSPENDED" && endlocal &&  bitsadmin /cancel download &&echo FILE WAS NOT ADDED && exit /b 3
  40.     bitsadmin /info download /verbose | find  "STATE: TRANSIENT_ERROR" && endlocal &&  bitsadmin /cancel download &&echo TRANSIENT ERROR && exit /b 4
  41.     bitsadmin /info download /verbose | find  "STATE: TRANSFERRED" && goto :finishing
  42.     sleep %timeout%
  43.     goto :repeat
  44.     :finishing
  45.     bitsadmin /complete download
  46.     echo download finished
  47.     endlocal
  48.     goto :eof
  49.    
  50.     :help
  51.     echo %~n0 url file [timeout]
  52.     echo.
  53.     echo  url - the source for download
  54.     echo  file - file name in local directory where the file will be stored
  55.     echo  timeout - number in seconds between each check if download is complete (attempts are 10)
  56.     echo.
  57.     goto :eof
Add Comment
Please, Sign In to add comment