AutoHotkey Community

It is currently May 26th, 2012, 12:27 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: How can I loop this?
PostPosted: September 21st, 2008, 3:05 pm 
Offline

Joined: September 1st, 2008, 2:50 am
Posts: 56
I need this script to loop, but done as shown, it makes about 50 entries in the input.txt file, presumably in the time it takes to create a new num.txt file, which is the trigger. Can anyone help me make it only enter the data once in input.txt, but still keep running the comparison until something changes again? :?

Code:
Loop
{
FileReadLine, transactionNum1, c:\sa\currslip.txt, 14 ; reads 14th line currslip.txt
FileReadLine, transactionNum2, c:\sa\num.txt, 1 ; reads 1st line of num.txt

StringLeft, transactionNum1, transactionNum1, 4 ; get the first 4 characters

If (transactionNum1!=transactionNum2)

FileRead, currslip, c:\sa\currslip.txt

If (currslip contains Credit Card) AND (currslip contains Amount)

{
StringReplace, currslip,currslip, `r`n, `n, All
StringReplace, currslip,currslip, `r, `n, All
StringSplit, L,currslip, `n

  needle := "Amount Tendered: "
  halfslip := SubStr(currslip, InStr(currslip, needle) + StrLen(needle))
  amount := SubStr(halfslip, 1, InStr(halfslip, "`n") - 1 )

  StringSplit, T, L14, %A_Space%
  output = 2=0`n1=%T1%`n3=%amount%`n99=0
  FileAppend, % output, c:\sa\input.txt
  Filedelete, c:\sa\num.txt
  FileAppend, %T1%, c:\sa\num.txt
}

else

If (currslip contains -) AND (currslip contains Credit Card)

{
StringReplace, currslip,currslip, `r`n, `n, All
StringReplace, currslip,currslip, `r, `n, All
StringSplit, L,currslip, `n

  needle := "-"
  halfslip := SubStr(currslip, InStr(currslip, needle,"",0) + StrLen(needle))
  amount := SubStr(halfslip, 1, InStr(halfslip, "`n") - 1 )

  StringSplit, T, L14, %A_Space%
  output = 2=6`n1=%T1%`n3=%amount%`n99=0
  FileAppend, % output, c:\sa\input.txt
  Filedelete, c:\sa\num.txt
  FileAppend, %T1%, c:\sa\num.txt
}
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 21st, 2008, 3:53 pm 
Offline

Joined: March 19th, 2006, 5:52 am
Posts: 419
Set up a timer that checks to see if something has changed. Then have that trigger your action to do the data work.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 21st, 2008, 3:56 pm 
if (expression) does not support "contains"

Try this:
Code:
currslip = pblabla - Credit Card
If (currslip contains -) AND (currslip contains Credit Card)
   msgbox, ok contains
if (instr(currslip, "-") AND instr(currslip, "Credit Card"))
   msgbox, ok instr


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 22nd, 2008, 12:06 am 
Offline

Joined: September 1st, 2008, 2:50 am
Posts: 56
I came up with this in the end, which seems to work well, no timer or sleep time seemingly needed, as script will wait for input file to be deleted by another program before doing it's thing again.

Is this script OK to use all day? Any improvements welcome!

Code:
Loop
{
 IfNotExist, c:\sa\input.txt
{
  Sleep, 1000
  FileReadLine, transactionNum1, c:\sa\currslip.txt, 14 ; reads 14th line currslip.txt
  FileReadLine, transactionNum2, c:\sa\num.txt, 1 ; reads 1st line of num.txt
  StringLeft, transactionNum1, transactionNum1, 4 ; get the first 4 characters

 If (transactionNum1!=transactionNum2)
{
  FileRead, currslip, c:\sa\currslip.txt

  StringReplace, currslip,currslip, `r`n, `n, All
  StringReplace, currslip,currslip, `r, `n, All
  StringSplit, L,currslip, `n

 If (instr(currslip,"Change")) AND (instr(currslip,"Credit Card"))
{
  needle := "Amount Tendered: "
  halfslip := SubStr(currslip, InStr(currslip, needle) + StrLen(needle))
  amount := SubStr(halfslip, 1, InStr(halfslip, "`n") - 1 )

  StringSplit, T, L14, %A_Space%
  output = 2=0`n1=%T1%`n3=%amount%`n99=0
  FileAppend, % output, c:\sa\input.txt
  Filedelete, c:\sa\num.txt
  FileAppend, %T1%, c:\sa\num.txt
}
 If (instr(currslip,"-")) AND (instr(currslip,"Credit Card"))
{
  needle := "-"
  halfslip := SubStr(currslip, InStr(currslip, needle,"",0) + StrLen(needle))
  amount := SubStr(halfslip, 1, InStr(halfslip, "`n") - 1 )

  StringSplit, T, L14, %A_Space%
  output = 2=20`n1=%T1%`n3=%amount%`n99=0
  FileAppend, % output, c:\sa\input.txt
  Filedelete, c:\sa\num.txt
  FileAppend, %T1%, c:\sa\num.txt
}
}
}
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 22nd, 2008, 1:47 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
Here's a shortened version which should have the same functionality. If it's not necessary to check for the input file every second, sleeping longer between loop iterations would be less processor intensive.

Code:
Loop
{
 Sleep, 1000     ;waits 1 second before looking for input file again
 IfExist, c:\sa\input.txt
  continue

 FileReadLine, transactionNum1, c:\sa\currslip.txt, 14     ;reads 14th line currslip.txt
 FileReadLine, transactionNum2, c:\sa\num.txt, 1           ;reads 1st line of num.txt

 If (SubStr(transactionNum1,1,4) != transactionNum2) OR !(InStr(currslip,"Credit Card"))
  continue

 FileRead, currslip, c:\sa\currslip.txt
 StringReplace, currslip,currslip, `r`n, `n, All
 StringReplace, currslip,currslip, `r, `n, All

 If InStr(currslip,"Change")
  halfslip := SubStr(currslip, InStr(currslip,"Amount Tendered: ") + 17), n := 0
 Else If InStr(currslip,"-")
  halfslip := SubStr(currslip, InStr(currslip,"-","",0) + 1), n := 20
 Else continue

 amount := SubStr(halfslip, 1, InStr(halfslip,"`n") - 1)
 StringSplit, T, transactionNum1, %A_Space%
 output = 2=%n%`n1=%T1%`n3=%amount%`n99=0
 FileAppend, % output, c:\sa\input.txt
 Filedelete, c:\sa\num.txt
 FileAppend, %T1%, c:\sa\num.txt
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: JamixZol, Morpheus, RUBn, SKAN, sks and 14 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group