Skip to content

Instantly share code, notes, and snippets.

@dhtml
Forked from WiVi71/BITS_DN.CMD
Created March 6, 2023 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhtml/937709fc15c9386c21c8f4a5320f07f8 to your computer and use it in GitHub Desktop.
Save dhtml/937709fc15c9386c21c8f4a5320f07f8 to your computer and use it in GitHub Desktop.
Simple batch file script to download file with progress bar using BITSAdmin
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F %%A IN ('COPY /Z "%~DPF0" NUL') DO SET "CR=%%A"
FOR /F %%B IN ('ECHO PROMPT $H ^| CMD') DO SET "BS=%%B"
(SET \N=^
%=_Empty_Line_=%
)
:BITS_DN
IF NOT [%2] == [] IF [%3] == [] GOTO BITS_SET
ECHO USAGE: BITS_DN.CMD "<URL>" "<FILE>"
ENDLOCAL & EXIT /B 1
:BITS_SET
ECHO BITS administration utility (C) Copyright Microsoft Corp.
ECHO Downloading "%~1"
:: Add commands to add bitsadmin settings.
BITSADMIN.EXE /cancel "BITS_DN Downloader" > NUL
BITSADMIN.EXE /create /Download "BITS_DN Downloader" > NUL
BITSADMIN.EXE /AddFile "BITS_DN Downloader" "%~1" "%~DPNX2" > NUL
BITSADMIN.EXE /SetPriority "BITS_DN Downloader" HIGH > NUL
BITSADMIN.EXE /SetNoProgressTimeout "BITS_DN Downloader" 30 > NUL
BITSADMIN.EXE /Resume "BITS_DN Downloader" > NUL && GOTO BITS_CONNECTING
BITSADMIN.EXE /cancel "BITS_DN Downloader" > NUL & ENDLOCAL & EXIT /B 1
:BITS_QUEUED
:BITS_CONNECTING
:: Add "CR" at the end of the line
FOR /F %%C IN ('BITSADMIN.EXE /RawReturn /GetState "BITS_DN Downloader"') DO (
SET "BITS_State=%%C ] "
SET /P "PRINT=Waiting to download... [ Current State : !BITS_State:~0,17!!CR!" < NUL
IF NOT "%%C" == "QUEUED" IF NOT "%%C" == "CONNECTING" ECHO:!\N!& GOTO BITS_%%C
)
PATHPING.EXE 127.0.0.1 -n -q 1 -p 250 > NUL
GOTO BITS_CONNECTING
:BITS_TRANSFERRING
BITSADMIN.EXE /SetNoProgressTimeout "BITS_DN Downloader" 30 > NUL
SET BITS_fB=#########################_________________________
SET BITS_File_Num=0
SET "BITS_FileN=%~NX2"
FOR /F %%C IN ('BITSADMIN.EXE /RawReturn /GetBytesTotal "BITS_DN Downloader"') DO SET /A "BITS_Tfs_int=BITS_Tfs=%%C"
FOR %%D IN (KiB MiB GiB) DO (
IF !BITS_Tfs_int! LSS 1048576 (
SET /A "BITS_Tfs_frac=BITS_Tfs_int%%1024*100/1024, BITS_Tfs_int/=1024"
SET BITS_Tunit=%%D
SET BITS_Tfs_frac=0!BITS_Tfs_frac!
GOTO BITS_Wait
)
SET /A BITS_Tfs_int/=1024
)
:BITS_Wait
FOR /F %%C IN ('BITSADMIN.EXE /RawReturn /GetState "BITS_DN Downloader"') DO SET BITS_State=%%C
FOR /F %%D IN ('BITSADMIN.EXE /RawReturn /GetBytesTransferred "BITS_DN Downloader"') DO (
IF %%D LSS 21474837 (SET /A "BITS_per=100*%%D/BITS_Tfs") ELSE (SET /A "BITS_per=%%D/(BITS_Tfs/100)")
SET /A "BITS_Rfs=%%D, BITS_Bnum=25-BITS_per/4"
)
FOR %%E IN (KiB MiB GiB) DO (
IF !BITS_Rfs! LSS 1048576 (
SET /A "BITS_Rfs_int=BITS_Rfs/1024, BITS_Rfs_frac=BITS_Rfs%%1024*100/1024"
SET BITS_Runit=%%E
SET BITS_Rfs_frac=0!BITS_Rfs_frac!
GOTO BITS_Wait2
)
SET /A BITS_Rfs/=1024
)
:BITS_Wait2
SET /P "PRINT=%BS% [!BITS_fB:~%BITS_Bnum%,25!] !BITS_per!%% ^| %BITS_Rfs_int%.%BITS_Rfs_frac:~-2%%BITS_Runit% / %BITS_Tfs_int%.%BITS_Tfs_frac:~-2%%BITS_Tunit% !CR!!BITS_FileN:~%BITS_File_Num%,13!!CR!" < NUL
IF NOT "%BITS_State%" == "TRANSFERRING" ECHO:!\N! & GOTO BITS_%BITS_State%
IF NOT "%BITS_FileN:~13,1%" == "" IF "!BITS_FileN:~%BITS_File_Num%,1!" == "" (SET BITS_File_Num=0) ELSE (SET /A BITS_File_Num+=1)
PATHPING.EXE 127.0.0.1 -n -q 1 -p 250 > NUL
GOTO BITS_Wait
:BITS_ACKNOWLEDGED
:BITS_TRANSFERRED
BITSADMIN.EXE /complete "BITS_DN Downloader" > NUL
IF %ERRORLEVEL% EQU 0 (
ECHO File "%~NX2" saved in "%~DP2"
) ELSE (
BITSADMIN.EXE /cancel "BITS_DN Downloader" > NUL
SET exitCode=3
)
ENDLOCAL & EXIT /B %exitCode%
:BITS_TRANSIENT_ERROR
ECHO %DATE% %TIME% [ERROR] - Recoverable error occurred
:BITS_ERWait
FOR /F %%C IN ('BITSADMIN.EXE /RawReturn /GetState "BITS_DN Downloader"') DO (
SET "BITS_State=%%C ] "
SET /P "PRINT=Waiting to receive bytes.. [ Current State : !BITS_State:~0,17!!CR!" < NUL
IF NOT "%%C" == "TRANSIENT_ERROR" (
BITSADMIN.EXE /SetNoProgressTimeout "BITS_DN Downloader" 90 > NUL
ECHO:& GOTO BITS_%%C
)
)
PATHPING.EXE 127.0.0.1 -n -q 1 -p 250 > NUL
GOTO BITS_ERWait
:BITS_ERROR
ECHO %DATE% %TIME% [ERROR] - Unrecoverable error occurred
BITSADMIN.EXE /RawReturn /GetError "BITS_DN Downloader"
BITSADMIN.EXE /cancel "BITS_DN Downloader" > NUL & ENDLOCAL & EXIT /B 2
:BITS_CANCELED
:BITS_SUSPENDED
ECHO %DATE% %TIME% [ERROR] - Unexpected state occurred
ECHO Download canceled or Wrong BITSAdmin parameters on label ":BITS_SET"
BITSADMIN.EXE /cancel "BITS_DN Downloader" > NUL & ENDLOCAL & EXIT /B 2

Introduction

This script is a simple batch file script that uses BITSAdmin to process download operations.

This script will add job to proceed with the download, instead of using the BITSAdmin /Transfer command.

Usage

CALL :BITS_DN "<URL>" "<FILE_PATH>" "<FILE_NAME>"

Example : CALL :BITS_DN "http://mirror.yongbok.net/gnu/binutils/binutils-2.34.tar.xz.sig" "C:\Temp" "binutils.tar.xz.sig"

Limit

File size : 2GB

If the file size reaches 2GB or more, the progress bar and file size are not properly marked.

Screen buffer

columns(mode con cols=*) :

Adding options

Editing progress bar

Description of the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment