[BATCH/.Bat文件]帮助计数错误吗? [英] [BATCH / .Bat files]Help counting up error?
问题描述
好吧,我试图让批处理文件每1秒计数一次,但是它可以部分地..
well im trying to have a batch file to count up every 1 second but it comes that it partially can ..
如果秒数少于10秒,我所做的代码将显示为01,02 ..而不是1,2 ..
the code i did was if seconds are lower than 10 than they will be displayed like 01,02 .. not like 1,2 ..
这是完整文件
@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
但是问题出在,而不是从00到10 ....每次都会在08重置.这是关于enabledelayedexpansion吗?和我如何解决/简化它,而无需下载外部.exe文件放入文件夹
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 ..
推荐答案
批处理软件时钟:
@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
- 1000毫秒而不是100
- 大于59
- 使用子字符串作为填充
- 使用 TEST-NET IP 192.0.2.2
注意:该时钟不是十分准确,并且像依赖ping超时的任何软件解决方案一样,很可能会衰减.衰减将由处理器处理所有命令所花费的时间超过一秒钟而导致.如果处理器处于负载状态并且批处理脚本优先级正常,则可以很容易看出这一点.但是,在处理器没有负载的大多数使用情况下,时钟应在几分钟和几秒钟内以足够不错的精度运行.请注意.
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.
请注意,我已将 ping
命令的用法编译为用于等待和延迟功能.
Notes that I have compiled on the usage of the ping
command for wait and delay functionality.
成功方法:
PING 127.0.0.1 -n 6 >nul
- 5秒延迟
-
ping
将6
回显发送到回送IP地址. -
ping
会在每次回声之间暂停1秒钟,共5秒钟. -
-n
不能小于2或没有延迟. - 此方法需要快速响应的有效IP地址.
- 这就是为什么选择localhost环回IP
127.0.0.1
的原因.
ping
sends6
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.
超时方法:
PING 192.0.2.2 -n 1 -w 200 >nul
- 200毫秒延迟
-
ping
发送1个回显-n 1
,请求超时为200毫秒-w 200
. - (使用此方法时,仅调整
-w
的值并将-n
保留为1
) - 此方法需要一个未使用的IP地址.因为它需要引起失败而导致延迟.
- 唯一可以保证不使用的IP地址是用户可以控制的私有IP地址范围或TEST-NET范围.
ping
sends 1 echo-n 1
with a request timeout of 200 milliseconds-w 200
.- ( only adjust the
-w
value and leave-n
as1
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.
私有IP地址范围:
Internet号码分配机构(IANA)保留了以下三个专用互联网IP地址空间的块:
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)
- 根据我的经验,
10.1.1.1
只是一个常用的专用IP地址,通常不使用.这就是为什么我在选择私有IP地址时更喜欢它的原因.否则,请使用TEST-NET IP地址. - 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. - myself
- chakrit
- Rob van der Woude
- RFC1918
- RFC3330
- Explaining DHCP server 1.1.1.1
- 根据时间延迟通知进行解释.
- 切换到TEST-NET IP地址.
- 添加了有关
ping
和IP地址的信息. - Expounded upon time decay notice.
- Switched to TEST-NET IP address.
- Added information regarding
ping
and IP addresses.
TEST-NET IP地址范围:
192.0.2.0/24
-该块被分配为"TEST-NET",用于文档和示例代码.它通常与供应商和协议中的域名example.com或example.net文档.此块中的地址不应出现在公共互联网.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.公共IP地址:
截至2010年1月,IP地址 1.1.1.1已由IP地址分配给APNIC.IANA .这意味着不能再保证将其作为未使用的IP地址.
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.
这篇关于[BATCH/.Bat文件]帮助计数错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
工作原理:
- That is why the localhost loopback IP
- 这就是为什么选择localhost环回IP
工作原理: