batch variable progress bar string manipulation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tsip
Posts: 1
Joined: 21 Jun 2013 12:01

batch variable progress bar string manipulation

#1 Post by tsip » 21 Jun 2013 12:05

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch variable progress bar string manipulation

#2 Post by foxidrive » 21 Jun 2013 12:48

Code: Select all

@echo off
for /L %%a in (1,1,10) do (
set /p =*<nul
ping localhost -n 2 >nul
)
pause

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch variable progress bar string manipulation

#3 Post by aGerman » 21 Jun 2013 12:49

I see the same and for whatever reason the dot in echo. is the key for that behaviour.
BTW
Never use echo. - it has a lot of disadvantages. Better use echo( even if it looks odd and you don't really need it in your code.

Code: Select all

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

As you can see I did some minor changes. But one thing is important: Never use double quotes for numeric comparisons.

Regards
aGerman

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: batch variable progress bar string manipulation

#4 Post by Endoro » 22 Jun 2013 02:21

the OP did some cross postings here and here

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: batch variable progress bar string manipulation

#5 Post by Ocalabob » 22 Jun 2013 18:25

@OP,
Some advice. Your calling Endoro a name ("copy cat") on another forum may curtail further help on your failed
attempt at a batch script on this forum. I suggest you refrain from such snarky comments when asking for help.

Your welcome!

Post Reply