字符串与数字使用autohotkey
问题描述:
我想获得我的尾巴功能抓住日志文件中的最后一行,并将其变成一个数字。这样我就可以在if条件下使用它。字符串与数字使用autohotkey
file = C:\Users\%A_UserName%\Documents\logTime.txt
Tail(k,file) ; Return the last k lines of file
{
Loop Read, %file%
{
i := Mod(A_Index,k)
L%i% = %A_LoopReadLine%
}
L := L%i%
Loop % k-1
{
IfLess i,1, SetEnv i,%k%
i-- ; Mod does not work here
L := L%i% "`n" L }
;Return L
;msgbox % Tail(1,file)
}
if条件
While (PrLoad > 5) ; Assign the Number you want.
{
If (Tail(1, file) = %A_Hour%%A_Min%)
{
msgBox is equal to Current Time %Tail(1, file)%
Sleep 60000
}
Else if (Tail(1, file) > %A_Hour%%A_Min%)
{
msgBox Tail(1, file) is greater then %A_Hour%%A_Min%
Sleep 60000
}
日志文件是由下面的步骤进行:
FileAppend, %A_Hour%%A_Min%`n, C:\Users\%A_UserName%\Documents\logTime.txt
我为确保IM传递功能失常到如果条件.. %L%
如何我可以将字符串变成一个数字,以便通过if语句进行比较吗?
答
我希望大家都知道的事实,Tail(1, file) > %A_Hour%%A_Min%
可能会导致意想不到的结果。假设%A_Hour %% A_Min%为1250
而尾(1,file)返回0105
。
01:05可能会在12:50后发生,但您的脚本将无法看到。
现在,您可以继续添加日历,月份和年份,但这仍不能消除所有问题。
这就是为什么大多数人使用时间戳只是表示自1970年以来已经过去了多少秒(或如此)的时间戳。
... AHK可以使用字符串,就好像它们是数字一样,所以根本不应该有任何问题。
试试这个:
logFile = C:\Users\%A_UserName%\Documents\logTime.txt
;create a new timestamp and add it to the log
timestamp := GetUnixTimestamp()
FileAppend, %timestamp% `n, %logFile%
;wait a second
Sleep, 1000
;create another timestamp
currentTimestamp := GetUnixTimestamp()
;get old timestamp from log
timestampFromLog := FileGetLastLine(logFile)
MsgBox, %timestampFromLog% - Last timestamp from the log `n%currentTimestamp% - Current timestamp
If (currentTimestamp > timestampFromLog)
MsgBox, Everything ran as expected!
GetUnixTimestamp() {
T := A_NowUTC
T -= 1970,s
Return T
}
FileGetLastLine(file) {
Loop, Read, %file%
lineCount := A_Index
FileReadLine, lastLine, %file%, %lineCount%
Return lastLine
}
答
您是否使用最新版本的AutoHotkey?如果没有,请从autohotkey.com或ahkscript.org下载最新版本
从我看到您使用的是伪阵列,这是旧样式。
在这里对象/数组的当前状态,读了起来:
http://ahkscript.org/docs/Objects.htm http://ahkscript.org/docs/objects/Object.htm
主要的问题,我看到的是%的在你的变量missuse。函数不需要%%的命令需要%%。
您有重复的在您的链接。一个应该去。 – EngrStudent