Counter
Moderator: DosItHelp
Counter
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.
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.
Re: Counter
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).
jeb
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
Re: Counter
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.
Re: Counter
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
jeb
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
Re: Counter
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 useCode: 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?
Re: Counter
hi 1+2=46,
I dont have an idea, why your counter stop, but do you try to cancel it with CTRL-C?
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
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
Re: Counter
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.
-
- Posts: 5
- Joined: 07 Apr 2010 06:49
Re: Counter
you can do it with the variable %time%, without ping
a simple loop and when it change the counter increases by one
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
)
Re: Counter
Hi ---saster---,
Yes this works, but ...
It uses 100% CPU time (one single CPU).
Therefore you should call ping (or something that wait for a while)
This is not perfect, because the ping needs more time than a second.
jeb
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
Re: Counter
Thank you both, ---saster---and jeb, but I already did it with milliseconds.
It works almost 100% perfect.
Here is the code:
It took a lot to get the right value: 465.
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.
Re: Counter
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