解释 2 楼 不得不爱 版主代码:
:: 『第 2 楼』: 不得不爱 版主代码:
SET /a a=%time:~0,2%*60+%time:~3,2%
if %a% geq 480 if %a% leq 1080 (goto 下一步)
GOTO :EOF
:下1步
..........
上面代码(
红色标示)是取 %time% 环境变量中的
小时(采用截取字符串的方法取得的)
假如: %time% 所示时间为:11:15:33.01 (即:11点15分),则 %time:~0,2% 就是取 %time% 变量中
从偏移量0开始处取两个字符。取到的就是
小时。
%time:~0,2%*60,把取到的小时再乘以60(分钟),因为一小时是60分钟,所以通过字符串截取功能取到的小时数要换算成总的分钟数,所以要再乘以60。因为将小时换算成分钟是为了方便比对某个时间的范围。
+%time:~3,2%,当取到小时并且让小时乘以60而换算成分钟还不行,因为还要得到 %time% 变量中的
分钟数,所以 %time:~3,2% (假如%time%时间为: 11:
15:33.01)那么就是从偏移量3开始(偏移量从0开始计算)取两个字符,这两个字符就是上面(蓝色标示)“15”。
因为小时数已换算成分钟了,再加上%time%里的分钟数,这样求出的当前时间的总分钟数才更准确。
换算得到总的分钟数就可以更方便简单的使用 IF 来计算(并且还可以进行加减计算等特殊时间或是以这种原理的对日期的范围操作……)。
leq 1080 (上面代码绿色标示),这里的 LEQ 是小于或等于的含义,它的帮助信息使用 IF /? 命令可以看到:
EQU - 等于
NEQ - 不等于
LSS - 小于
LEQ - 小于或等于
GTR - 大于
GEQ - 大于或等于
上面代码含义是指:小于或等于 1080 分钟(18点:就是从0点到晚上6点共有18个小时*每小时60分钟,就是1080分钟)
然后以分钟做为对比的基础单位:
IF 当前时间大于或等于480分钟(即480/60=8小时,就是上午8点) 并且 当前时间小于或等于1080分钟(1080/60=18小时,就是晚18点),只要时间的范围在这之间,这就是楼主要求的:当前时间如果在早8点——晚18点之间,就执行某个命令……
(截取字符串知识点: SET /? 的帮助信息中有)
(%time%的知识点: SET /? 的帮助信息的最后面有关于可以“调用”的动态环境变量)
(判断大小与判断的知识点: IF /? 的帮助信息中)
(Goto的知识点: GOTO /? 的帮助信息中)
对于日期也可以使用类似的全转成数值的方法来判定某个文件的时间属性:属于在某个日期到某个日期的范围之内等特殊应用。
Last edited by redtek on 2007-1-26 at 12:10 PM ]
Explanation of the moderator code on the second floor:
:: 『Second floor』: Moderator code of Budenbuai:
SET /a a=%time:~0,2%*60+%time:~3,2%
if %a% geq 480 if %a% leq 1080 (goto next step)
GOTO :EOF
:next step
..........
The above code (marked in
red) is to take the
hour in the %time% environment variable (obtained by intercepting the string method)
For example: If the time shown in %time% is: 11:15:33.01 (that is: 11 o'clock and 15 minutes), then %time:~0,2% is to take two characters from the offset 0 start in the %time% variable. What is taken is the
hour.
%time:~0,2%*60, multiply the obtained hour by 60 (minutes), because one hour is 60 minutes, so the hour number obtained through the string interception function needs to be converted into the total number of minutes, so it needs to be multiplied by 60 again. Because converting hours to minutes is for the convenience of comparing a certain time range.
+%time:~3,2%, when the hour is obtained and the hour is multiplied by 60 to convert it into minutes, it is not enough, because the
minute number in the %time% variable is also needed, so %time:~3,2% (if the %time% time is: 11:
15:33.01) then it is to take two characters starting from offset 3 (offset is calculated from 0), these two characters are the above (blue marked) "15".
Because the number of hours has been converted into minutes, and then adding the number of minutes in %time%, the total number of minutes of the current time obtained in this way is more accurate.
Converting to the total number of minutes can make it more convenient and simple to use IF for calculation (and can also perform addition and subtraction calculations and other special times or operations on the range of dates based on this principle...).
leq 1080 (marked in green in the above code), here LEQ means less than or equal to. Its help information can be seen by using the IF /? command:
EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal to
GTR - greater than
GEQ - greater than or equal to
The meaning of the above code refers to: less than or equal to 1080 minutes (18 o'clock: there are 18 hours from 0 o'clock to 6 o'clock in the evening, which is 18 hours * 60 minutes per hour, which is 1080 minutes)
Then use minutes as the basic unit for comparison:
IF the current time is greater than or equal to 480 minutes (that is, 480/60=8 hours, which is 8 o'clock in the morning) and the current time is less than or equal to 1080 minutes (1080/60=18 hours, which is 18 o'clock in the evening), as long as the time range is between these, this is what the building owner requires: if the current time is between 8 o'clock in the morning - 18 o'clock in the evening, execute a certain command...
(Intercepting string knowledge: There is in the help information of SET /?)
(Knowledge of %time%: There is at the end of the help information of SET /? about the dynamic environment variables that can be "called")
(Knowledge of judging size and judgment: There is in the help information of IF /?)
(Knowledge of Goto: There is in the help information of GOTO /?)
For dates, a similar method of converting all to numerical values can also be used to determine the time attributes of a certain file: special applications such as being within a certain date range.
Last edited by redtek on 2007-1-26 at 12:10 PM ]