AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How can I loop this?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
artbasement



Joined: 01 Sep 2008
Posts: 52

PostPosted: Sun Sep 21, 2008 2:05 pm    Post subject: How can I loop this? Reply with quote

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? Confused

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
}
}
Back to top
View user's profile Send private message MSN Messenger
TheIrishThug



Joined: 19 Mar 2006
Posts: 417

PostPosted: Sun Sep 21, 2008 2:53 pm    Post subject: Reply with quote

Set up a timer that checks to see if something has changed. Then have that trigger your action to do the data work.
Back to top
View user's profile Send private message Visit poster's website AIM Address
FGR
Guest





PostPosted: Sun Sep 21, 2008 2:56 pm    Post subject: Reply with quote

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
Back to top
artbasement



Joined: 01 Sep 2008
Posts: 52

PostPosted: Sun Sep 21, 2008 11:06 pm    Post subject: Reply with quote

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
}
}
}
}
Back to top
View user's profile Send private message MSN Messenger
jaco0646



Joined: 07 Oct 2006
Posts: 1770
Location: MN, USA

PostPosted: Mon Sep 22, 2008 12:47 am    Post subject: Reply with quote

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
}

_________________
http://autohotkey.net/~jaco0646/
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group