progress bar in batch string manipulation [duplica

2019-08-15 10:44发布

问题:

This question already has an answer here:

  • batch variable string manipulation not working 1 answer

Here's my code, (i tried to make a progress bar but failed). http://pastebin.com/JpUT3QvY I get this as output: bar:~0,1 bar:~0,2 bar:~0,3 etc.. etc.. I want it to go like:*, **, *** etc.. basically increase the asterisk by 1 every 1 second. Any help is much appreciated, i tried many different structures but failed. Now i need to keep writing just so this filter can accept my question since it wants me to write more using proper grammar now you can stop reading as this is completely irrelevant to the question.

@echo off & setlocal enabledelayedexpansion
    set bar=**********
    set cnt=0
    :LOOP
    cls
    set /A cnt+=1
    echo.Progress:!bar:~0,%cnt%!
    ping -n 1 www.google.com > nul 2>&1
    if "%cnt%" NEQ 10 goto :LOOP
    echo.finished.
    pause > nul
    exit /b

回答1:

try this:

@echo off & setlocal enabledelayedexpansion
set "bar=**********"
set /a cnt=0
:LOOP
cls
set /A cnt+=1
SET "progressbar=!bar:~0,%cnt%!"
ECHO(Progress:%progressbar%
ping -n 1 www.google.com > nul 2>&1
if %cnt% NEQ 10 goto :LOOP
ECHO(finished.

REM example without CLS
<NUL set/p "=Progress:"
:LOOPING
<NUL set/p"=*"
ping -n 1 www.google.com > nul 2>&1
SET /a count+=1
if %count% NEQ 10 goto :LOOPING
ECHO(&ECHO(finished.