Increment a progress bar in a windows10 batch file
I've excerpted a part of the batch file that I'm using. I was experimenting with making progress bars, instead of numerical counts output by timeout.
My question is, how can I dynamically modify the string in a loop, rather than having to retype every step of the string?
@echo off
setlocal enabledelayedexpansion
:: This is to set variables that represent backspace and carriage return.
:: I begin strings with these, instead of ending strings with only CR,
:: so that the cursor isn't flashing under the first character of the
:: progress bar. Beginning each string with a space, and ending each
:: with !CR! only is another possible workaround.
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
for /f %%a in ('"prompt $H&for %%b in (0) do rem"') do set "BS=%%a"
<nul set /p"= [ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■■ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■■■ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■■■■ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■■■■■ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■■■■■■ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■■■■■■■ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■■■■■■■■ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■■■■■■■■■ ]" & Timeout /t 1 >nul
<nul set /p"=!BS!!CR![■■■■■■■■■■]" & Timeout /t 1 >nul
As you can see, I currently type eleven commands (well, copy/paste then modify each line) to create the progress of a progress bar.
I would prefer to have a loop that adjusts the width of the bar to the number of times it will loop, then replaces the leftmost space with a block each loop. It would let me re-use this for a wait of any length, or even to show the progress of batch processes that I don't want to flood the screen with, without needing extensive modifications each time.
Unfortunately, I'm not honestly very knowledgeable about batch; I know enough to hack things together with some Google-Fu, but my search terms are not giving me good results with this, and I am not sure that what I'm trying to do is feasible. Any help, even just a starting point, would be appreciated.
I'm not sure I understand your question, but here's a suggestion based on substring and for /l
loop:
@echo off
>nul chcp 65001
set "_spc= "
set "_bar=■■■■■■■■■■"
setlocal enabledelayedexpansion
for /f %%a in ('copy /Z "%~dpf0" nul')do for /f skip^=4 %%b in ('echo;prompt;$H^|cmd')do set "BS=%%~b" & set "CR=%%a"
for /l %%L in (1 1 10)do <con: set /p "'= !CR!!BS!!CR![!_bar:~0,%%~L!!BS!!_spc:~%%~L!] " <nul & >nul timeout.exe /t 1
endlocal & goto :eof
- Or some like:
@echo off
>nul chcp 65001
setlocal enabledelayedexpansion
set "_spc= "
set "_bar=■■■■■■■■■■"
set "_msg= Progress bar loop."
<con: color 0A & mode 70,4 & echo\
for /f %%a in ('copy/Z "%~dpf0" nul')do for /f skip^=4 %%b in ('echo;prompt;$H^|cmd')do set "BS=%%b" & set "CR=%%a"
for /l %%L in (1 1 10)do <con: set/p "'=!CR!!BS!!CR![!_bar:~0,%%~L!!BS!!_spc:~%%L!]!_msg!"<nul & >nul timeout.exe 1
endlocal & color & goto :eof
Additional Resources:
Set /?
For /?
For /L /?
Set variable=variable:substrings
| DOS - String Manipulation