问题
I would like to get my tail function to grab the last line in logfile and turn it into a number. So that I can then use it in a if condition.
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)
}
The if condition
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
}
Logfile is being made by the following:
FileAppend, %A_Hour%%A_Min%`n, C:\Users\%A_UserName%\Documents\logTime.txt
I am as sure im passing the function wrong into the if condition..%L%
how can I turn the string into a number to be compared by the if statments?
回答1:
I hope you are aware of the fact that Tail(1, file) > %A_Hour%%A_Min%
may lead to unexpected results.
Let's say %A_Hour%%A_Min% is 1250
and Tail(1, file) returns 0105
.
01:05 may take place after 12:50, but your script would fail to see that.
Now you could go on and add the day, month and year to it, but that would still not eliminate all issues.
That's why most people use timestamps which just respresent how many seconds have passed by since 1970 (or so).
...
AHK can work with strings as if they are numbers, so there shouldn't be any problems at all with that.
Give this a try:
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
}
回答2:
Are you using the latest version of AutoHotkey? If not please download the latest version from autohotkey.com or ahkscript.org
From what I see you are using Pseudo Arrays, which is Old Style.
Read up on current state of Objects/Arrays here:
http://ahkscript.org/docs/Objects.htm http://ahkscript.org/docs/objects/Object.htm
The main issue I see is a missuse of %'s around your variables. Functions don't need %%'s Commands need %%.
http://ahkscript.org/docs/Tutorial.htm#s5
来源:https://stackoverflow.com/questions/32958438/string-to-number-using-autohotkey