Page 1 of 1

Extract line number from clipboard / text file.

Posted: 15 Jan 2018, 06:11
by Knotty_
Morning All,

I'm trying to extract the line number from an auto generated text file to determine quantity of items (ie if string is on line 9 this means there is one number, if string is on line 11 this means there is 3 number).

I looked through the forums and found something similar on the archived forums:

https://autohotkey.com/board/topic/1006 ... le-please/

Below is how I have tried to implement it:

Code: Select all

F11::

Send ^a
Send ^c

string := "Subtotal"

Loop, Parse, clipboard, `r`n
{
If A_loopfield contains %string%
  {
  LineNum := A_Index
break
  }
}
MsgBox %LineNum%
return
Odd thing is it returns 15 regardless of what text file I look at (be in just an 'a' or a full text document). I use it by making the notepad window active and hitting F11.

Re: Extract line number from clipboard / text file.

Posted: 15 Jan 2018, 06:50
by noname
Try to debug by running the code to see what whent wrong.

Code: Select all

F11::

Send ^a
Send ^c

string := "Subtotal"

Loop, Parse, clipboard, `r`n
{
If A_loopfield contains %string%
LineNum := A_Index
l .= A_Index "    "   A_LoopField "`n"
}
MsgBox linenumber= %LineNum%`n**********************`n%l%
return

Re: Extract line number from clipboard / text file.

Posted: 15 Jan 2018, 06:58
by Knotty_
Thanks for that, for some reason there is an additional return in the %clipboard% between lines that doesn't show in the text file.

Eg (text file):

1 sometext
2 sometext
3 sometext
4 subtotal

%clipboard%

1 sometext
2
3 sometext
4
5 sometext
6
7 subtotal

Re: Extract line number from clipboard / text file.

Posted: 15 Jan 2018, 07:03
by Knotty_
Right, turns out it was the use of 'r, it was adding returns between lines.

Re: Extract line number from clipboard / text file.

Posted: 15 Jan 2018, 07:29
by noname

Code: Select all

Loop, Parse, clipboard, `n,`r
Probably you solved it already :)
Greetings,