xDeathwing
xDeathwing

Reputation: 139

[BATCH / .Bat files]Help counting up error?

well im trying to have a batch file to count up every 1 second but it comes that it partially can ..

the code i did was if seconds are lower than 10 than they will be displayed like 01,02 .. not like 1,2 ..

here is the full file

@Echo off
set m=00
set m1=00 //m1 being minutes and m being seconds
echo %m% 
:clock
set /a m+=1
if %m% LSS 10 ( if %m% GTR 0 ( set m=0%m%)) ELSE (break) // for detecting if less than 10 found out that break doesnt do any job
if %m% GTR 60 (set /a m1+=1) //60 seconds will make it a minute .. 
if %m1% LSS 10  ( if %m1% GTR 0 (set m1=0%m1% ) ) //same detecting for minutes..
echo %m% : %m1% // display time ss:mm


ping 1.1.1.1 -n 1 -w 100 > nul //stop the file for 100 (1 secs)
goto clock // go to your looping clock

but the problem is instead of counting from 00 to 10 .... it resets at 08 everytime.. is this about enabledelayedexpansion ?? and how can i fix / simplisize it without downloading external .exe files to put in the folder

the weird thing is the following code works without no problems

if %m% LSS 10 ( echo 0%m%) else ( echo %m%)

but if i use it like the top i would need to go complicated to write minutes or even hours ..

Upvotes: 1

Views: 350

Answers (1)

David Ruhmann
David Ruhmann

Reputation: 11367

###Batch Software Clock:

@echo off
setlocal
set "s=0"
set "m=0"
:clock
set "mm=00%m%"
set "ss=00%s%"
echo %mm:~-2% : %ss:~-2%
set /a "s+=1"
if %s% gtr 59 set "s=0" & set /a "m+=1"
ping 192.0.2.2 -n 1 -w 1000 >nul
goto clock
endlocal

Note: that this clock is not perfectly accurate and will most likely have decay as with any software solution relying upon a ping timeout. The decay will result from the processor taking longer than a second to process the all the commands. This can be easily seen if the processor is under load and the batch script priority is just normal. However, in most use cases where the processor is not under load, the clock should perform with decent enough accuracy for minutes and seconds. Just be aware.


###About Ping Delay:

Notes that I have compiled on the usage of the ping command for wait and delay functionality.

Success Method:

PING 127.0.0.1 -n 6 >nul
  • 5 Second Delay

How it Works:

  • ping sends 6 echos to the loopback IP address.
  • ping pauses for 1 second pause between each echo totaling 5 seconds.
  • -n cannot be less than 2 or there will be no delay.
  • This method requires a fast and responsive valid IP address.
  • That is why the localhost loopback IP 127.0.0.1 is chosen.
  • Precision in seconds. Minimum delay of 1 second.

Timeout Method:

PING 192.0.2.2 -n 1 -w 200 >nul
  • 200 Millisecond Delay

How it Works:

  • ping sends 1 echo -n 1 with a request timeout of 200 milliseconds -w 200.
  • ( only adjust the -w value and leave -n as 1 when using this method )
  • This method requires an unused IP address. Because it needs to fail to cause the delay.
  • The only IP address that can be guaranteed to be unused are the private IP address ranges over which the user has control, or the TEST-NET range.
  • Precision in hundreds of milliseconds. Minimum delay of 100 milliseconds.

Private IP Address Range:

The Internet Assigned Numbers Authority (IANA) has reserved the following three blocks of the IP address space for private internets:

 10.0.0.0        -   10.255.255.255  (10/8 prefix)
 172.16.0.0      -   172.31.255.255  (172.16/12 prefix)
 192.168.0.0     -   192.168.255.255 (192.168/16 prefix)
  • In my experience 10.1.1.1 is just a common private IP address that is usually left unused. That is why I prefer it when selecting a private IP address. Otherwise, use a TEST-NET IP address.

TEST-NET IP Address Range:

192.0.2.0/24 - This block is assigned as "TEST-NET" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol documentation. Addresses within this block should not appear on the public Internet.

Public IP Addresses:

As of January 2010, IP Address 1.1.1.1 has been assigned to APNIC by the IANA. This means that is can no longer be guaranteed as an unused IP address.


###References


###Update:

  • Expounded upon time decay notice.
  • Switched to TEST-NET IP address.
  • Added information regarding ping and IP addresses.

Upvotes: 2

Related Questions