batch variable string manipulation not working

2019-08-31 01:17发布

Here's my code, (i tried to make a progress bar but failed).

@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

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 second.

1条回答
做个烂人
2楼-- · 2019-08-31 01:55

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.
查看更多
登录 后发表回答