Counter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Counter

#1 Post by 1+2=46 » 08 Apr 2010 23:14

Hi.

I am making a batch file that has a counter on it. Before I tried and changed some things in the code, it was all right, but after I changed the code when I open the batch file it just instantly closes.

Here's my code:

@echo off
SetLocal EnableDelayedExpansion
Cls
Color 0C
Cls
Title
Cls

Set /A seconds=0
Set /A minutes=1
:Loop
Cls
Set /A seconds=%seconds% - 1
If %seconds%==-1 (
Set /A seconds=59
Set /A minutes=%minutes% - 1
If %minutes%==-1 goto End
If %seconds%==9 goto No_01
If %seconds%==8 goto No_01
If %seconds%==7 goto No_01
If %seconds%==6 goto No_01
If %seconds%==5 goto No_01
If %seconds%==4 goto No_01
If %seconds%==3 goto No_01
If %seconds%==2 goto No_01
If %seconds%==1 goto No_01
If %seconds%==0 goto No_01
If %minutes%==9 goto No_02
If %minutes%==8 goto No_02
If %minutes%==7 goto No_02
If %minutes%==6 goto No_02
If %minutes%==5 goto No_02
If %minutes%==4 goto No_02
If %minutes%==3 goto No_02
If %minutes%==2 goto No_02
If %minutes%==1 goto No_02
If %minutes%==0 goto No_02
Echo %minutes%:%seconds%
Ping localhost -n 2 -w 1000 >nul
Goto Loop

:No_01
Echo %minutes%:0%seconds%
Ping localhost -n 2 -w 1000 >nul
Goto Loop

:No_02
Echo 0%minutes%:%seconds%
Ping localhost -n 2 -w 1000 >nul
Goto Loop

:End
Cls
Exit
Cls


Help me, please.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Counter

#2 Post by jeb » 09 Apr 2010 01:43

Hi 1+2=46,

you simply forgot the )"

But you can "pimp" your code a bit.

And for better effects you should look at http://www.dostips.com/forum/viewtopic.php?f=3&t=939 "Solution: to write multiple times on the same line" (without clear the screen).

Code: Select all

@echo off
SetLocal EnableDelayedExpansion
Cls
Color 0C
Cls
Title
Cls

Set /A seconds=2
Set /A minutes=10

:Loop
:: Cls
call :display
Ping localhost -n 2 -w 1000 >nul
set /A seconds=%seconds% - 1
if %seconds%==-1 (
   Set /A seconds=59
   Set /A minutes=%minutes% - 1
   if !minutes!==-1 goto :eof
)
goto :loop

:display
setlocal
set dispMin=0%minutes%
set dispSec=0%seconds%
cls
echo %dispMin:~-2%:%dispSec:~-2%
(
endlocal
goto :eof
)



jeb

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Counter

#3 Post by 1+2=46 » 09 Apr 2010 09:57

jeb wrote:Hi 1+2=46,

you simply forgot the )"

But you can "pimp" your code a bit.

And for better effects you should look at http://www.dostips.com/forum/viewtopic.php?f=3&t=939 "Solution: to write multiple times on the same line" (without clear the screen).

Code: Select all

@echo off
SetLocal EnableDelayedExpansion
Cls
Color 0C
Cls
Title
Cls

Set /A seconds=2
Set /A minutes=10

:Loop
:: Cls
call :display
Ping localhost -n 2 -w 1000 >nul
set /A seconds=%seconds% - 1
if %seconds%==-1 (
   Set /A seconds=59
   Set /A minutes=%minutes% - 1
   if !minutes!==-1 goto :eof
)
goto :loop

:display
setlocal
set dispMin=0%minutes%
set dispSec=0%seconds%
cls
echo %dispMin:~-2%:%dispSec:~-2%
(
endlocal
goto :eof
)



jeb



Thank you. It worked.

Also, do you have any idea how can I make it play a beep every time the seconds change?
What I do need is the beep. Like in real non-virtual counters.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Counter

#4 Post by jeb » 09 Apr 2010 11:59

Hi %~"1+2=46"%,

look at "Convert decimal to hex, and bytes to chars", http://www.dostips.com/forum/viewtopic.php?f=3&t=951

Then you can use

Code: Select all

call :byteList2string bell 7
echo %bell%


jeb

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Counter

#5 Post by 1+2=46 » 09 Apr 2010 15:26

jeb wrote:Hi %~"1+2=46"%,

look at "Convert decimal to hex, and bytes to chars", http://www.dostips.com/forum/viewtopic.php?f=3&t=951

Then you can use

Code: Select all

call :byteList2string bell 7
echo %bell%


jeb


Never mind, I don't need it anymore. Thanks, anyway.

Do you know how can I make the seconds 100% exactly?
I already tried use milliseconds, but it still isn't 100% exact.

Also, today I put the minutes in 60 and went do some shit. When I came back (after a few minutes) the counter has stopped. It was about 47:02. Any ideas how this happened?

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Counter

#6 Post by jeb » 09 Apr 2010 15:41

hi 1+2=46,

I dont have an idea, why your counter stop, but do you try to cancel it with CTRL-C?

Do you know how can I make the seconds 100% exactly?
I already tried use milliseconds, but it still isn't 100% exact.


I suppose, you can not achieve it with the ping command only, but with the %time% and wait with your own command.
Or you can use the ping command, and syncronize with the absolute %time%.
But then you can miss some seconds in the display.

Example:
Displaying
10:20
10:19
10:17

jeb

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Counter

#7 Post by 1+2=46 » 09 Apr 2010 15:53

jeb wrote:hi 1+2=46,

I dont have an idea, why your counter stop, but do you try to cancel it with CTRL-C?

Do you know how can I make the seconds 100% exactly?
I already tried use milliseconds, but it still isn't 100% exact.


I suppose, you can not achieve it with the ping command only, but with the %time% and wait with your own command.
Or you can use the ping command, and syncronize with the absolute %time%.
But then you can miss some seconds in the display.

Example:
Displaying
10:20
10:19
10:17

jeb


How can I do that?

And, maybe the counter stopped because I wasn't on the PC, so it wasn't active.

---saster---
Posts: 5
Joined: 07 Apr 2010 06:49

Re: Counter

#8 Post by ---saster--- » 10 Apr 2010 04:50

you can do it with the variable %time%, without ping

a simple loop and when it change the counter increases by one

Code: Select all

@echo off
set seg=0
:a
set "var=%time:~6,2%"
set "b=%var%"
:b
set var=%time:~6,2%
if ["%var%"]==["%b%"] (goto b) else (
set /a "seg+=1"
call echo %%seg%%
goto a
)

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Counter

#9 Post by jeb » 10 Apr 2010 05:30

Hi ---saster---,

a simple loop and when it change the counter increases by one


Yes this works, but ...

It uses 100% CPU time (one single CPU).

Therefore you should call ping (or something that wait for a while)

Code: Select all

@echo off
set seg=0
:a
set "var=%time:~6,2%"
set "b=%var%"
:b
set var=%time:~6,2%
if ["%var%"]==["%b%"] (goto b) else (
set /a "seg+=1"
call echo %%seg%%
ping -n 2 127.0.0.1 > nul
goto a
)


This is not perfect, because the ping needs more time than a second.

jeb

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Counter

#10 Post by 1+2=46 » 10 Apr 2010 07:30

Thank you both, ---saster---and jeb, but I already did it with milliseconds.

It works almost 100% perfect.


Here is the code:

Code: Select all

@echo off
SetLocal EnableDelayedExpansion
Cls
Color 0C
Cls
Title 
Cls

Set /A milliseconds=0
Set /A seconds=0
Set /A minutes=5

:Loop
Set /A milliseconds=%milliseconds% + 1
If %seconds%==0 (
   Set /A seconds=60
   Set /A minutes=%minutes% - 1
   If !minutes!==-1 goto :Command
)
If %milliseconds% GTR 465 (
   Set /A milliseconds=0
   Set /A seconds=%seconds% - 1
   Call :Display
)
Goto Loop

:Display
SetLocal
Set DispMin=0%minutes%
Set DispSec=0%seconds%
Cls
Echo %DispMin:~-2%:%DispSec:~-2%
(
EndLocal
Goto :eof
)

:Command
Exit
Cls


It took a lot to get the right value: 465.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Counter

#11 Post by !k » 10 Apr 2010 07:36

jeb wrote:Yes this works, but ...

Even less CPU usage

Code: Select all

@echo off
echo wscript.sleep wscript.arguments(0)>sleep.vbs
set seg=0
:a
set "var=%time:~6,2%"
set "b=%var%"
:b
set var=%time:~6,2%
if ["%var%"]==["%b%"] (goto b) else (
set /a "seg+=1"
call echo %%seg%%
cscript sleep.vbs 850>nul
goto a
)

The value "850" may have to adjust to your system

Post Reply