1
votes

I have a job that I want to assign it to AHK to run for me in 5 minute intervals. It needs to check the contents of a txt file, lets say c:\test.txt. This file can contain any number of lines in the format of:

prog 1
prog 2
prog 3
  ...
prog N

among lines containing other pieces of text.

My purpose is to tell AHK the number right after the word prog, on the last line, which is the largest number in the list as these lines come in numeric sequence in this file.

My grandiose idea is to check the existence of lines from "prog 1" to "prog N", where N is a number under 20, using a series of find commands on a cmd window, get the errorlevel back and when errorlevel hits 1, I will set my desired variable to the value of the value of the loop index minus 1, as it was the last successfully ran find command, as such

i=1
loop 20
{
  type c:\users\me\test.txt | find "prog "%i%
  if  %errorlevel% != 0 
  { 
     num := i-1
     ; exit the loop in some way here
  }
  else 
  {
     i := i+1
  }
 } ; end loop

At this point, all I am interested in is the value stored in variable %num%. I know it may not be the most elegant solution to accomplish this but for a few runs everyday on an otherwise idle laptop, I can take the performance penalties.

So far, I am stuck at the very basic component of this idea: find command and passing the errorlevel it generates back to AHK

here is my code: (this code is only for the testing of find command, which I got from AHK forums)

Run %COMSPEC% /K type c:\users\me\test.txt | find "prog 4", , max
msgbox %ErrorLevel%
return

when this piece of code runs, the messagebox always displays the value "0" regardless "prog 4" line is there or not.

when I replace command Run with RunWait, then errorlevel gets displayed as one expects, but in that case, my automation process runs into a glitch: Expecting manual effort to close the cmd prompt, which I am assuming the *feature" of the Wait portion of the command RunWait.

How can I get over this hurdle ? I am open to loading the whole functionality to a dos batch file and getting a numeric output from the batch file at the end, but I came to realize, programming with AHK is much easier to do and to understand than that of DOS batch programming. So, my preference is on the AHK scripting side.

Thank you

3

3 Answers

1
votes

If you are wanting to do all of this within AutoHotkey, this is how I would do it

FileRead, Content, C:\test.txt
Loop, parse, Content, `n
{
    RegExMatch(A_Loopfield, "(?i)(?<=prog\s)[\d]+", match)
    If (match > lastMatch)
        lastMatch := match
}

msgbox % lastMatch

Edited to always get highest value

1
votes

I would just use RegEx to extract the last item:

FileRead, cont, test.txt
RegExMatch(cont, "s).*prog (\d+)", num)
msgbox, Hightest number: %num1%

AutoHotkey should be sufficient most of the time when it comes to arithmetics/string operations. Calling other applications for simple things like this is kind of an overkill and will most likely slow you down.

Explanation for the RegEx:

  • s) determines that the period (.) will match each character including any newline (\r and/or \n). By default, it would stop at the first line break. See RegEx options.
  • .* is a standard pattern that will match anything. It is the key to find the last item in our string, because it will consume as much as possible as long as the pattern is still satisfied. In other words, it will discard everything up until the last prog n, which is exactly what we want. See Greed in the RegEx reference.
  • (\d+) does 2 things: 1) It will find any integer number sequence 2) It will create a subpattern and store the number where we can explicitely access it later.

The output var (here: num) will be a pseudo array containing any matched subpattern plus the entire pattern that was matched. Printing %num% will show you almost the whole file, up to the last prog n. %num1% will contain the first subpattern, which is the biggest n.

1
votes

Just let the system get the value for you.

RunWait,%comspec% /v:on /c "( for /f "tokens=2" `%f in ('findstr /b /r /c:"prog [0-9]+" c:\users\me\test.txt ') do set max=`%f ) & exit /b !max!",,UseErrorLevel
MsgBox %ErrorLevel%